我有一个数据框,其中一列('ProcessingDATE')是日期时间格式。我想创建另一列(“报告日期”),如果处理日期是星期一,则从中减去3天,最后以星期五结束;否则减去1天。
我使用python的时间很短,因此对如何编写它没有太多的了解。我的想法是用"provider": {
"@type": "LocalBusiness",
"@id": "https://example.com/#LocalBusiness"
}
,cell = Monday
编写一个for循环; then = datetime.datetime.today() – datetime.timedelta(days=3)
else = datetime.datetime.today() – datetime.timedelta(days=1)
答案 0 :(得分:0)
希望这会有所帮助,
import json
import requests
from requests.exceptions import HTTPError
host = "http://httpbin.org/post"
host = "http://webservicehere.azurewebsites.net"
f = open('hard_code.xml', 'rb').read()
try:
response = requests.head(host,data=f)
response.encoding = "utf-8"
print("Encoding " + response.encoding)
#response = requests.get('https://api.github.com/search/repositories',
#params={'q': 'requests+language:python'})
response.raise_for_status()
print(response.status_code)
print(response.text)
if 'json' in response.headers.get('Content-Type'):
json_response = response.json()
print(json_response)
else:
print("Response not in JSon form")
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Other error occurred: {err}') # Python 3.6
else:
print('Success!')
以上内容也可以写成
from datetime import timedelta
if DDA_compamy['ProcessingDATE'].weekday() == 4: #Condition to check if it is friday
DDA_compamy['Report Date']=DDA_compamy['ProcessingDATE'] - timedelta(days=3) # if friday subtracting 3 days
else:
DDA_compamy['Report Date']=DDA_compamy['ProcessingDATE'] - timedelta(days=1) #Else one day from the date is subtracted
答案 1 :(得分:0)
使用pandas.Series.dt.weekday和一些逻辑:
import pandas as pd
df = pd.DataFrame({'ProcessingDATE':pd.date_range('2019-04-01', '2019-04-27')})
df1 = df.copy()
mask = df1['ProcessingDATE'].dt.weekday == 0
df.loc[mask, 'ProcessingDATE'] = df1['ProcessingDATE'] - pd.to_timedelta('3 days')
df.loc[~mask, 'ProcessingDATE'] = df1['ProcessingDATE'] - pd.to_timedelta('1 days')
输出:
ProcessingDATE
0 2019-03-29
1 2019-04-01
2 2019-04-02
3 2019-04-03
4 2019-04-04
5 2019-04-05
6 2019-04-06
7 2019-04-05
8 2019-04-08
9 2019-04-09
10 2019-04-10
11 2019-04-11
12 2019-04-12
13 2019-04-13
14 2019-04-12
15 2019-04-15
16 2019-04-16
17 2019-04-17
18 2019-04-18
19 2019-04-19
20 2019-04-20
21 2019-04-19
22 2019-04-22
23 2019-04-23
24 2019-04-24
25 2019-04-25
26 2019-04-26