这是我尝试过的和得到的:
start = datetime.datetime.strptime("1973-01-01", "%Y-%m-%d")
moving = start
d = {}
year =0
month =0
while moving < datetime.datetime(1982,7,1):
# print moving
if moving.year == year:
if moving.month == month:
if moving.weekday() < 5:
d[str(moving.year)+"-"+ str(moving.month)] += 1
else:
d[str(moving.year)+"-"+ str(moving.month)]=0
if moving.weekday() < 5:
d[str(moving.year)+"-"+ str(moving.month)] += 1
else:
# d[moving.year] = {}
d[str(moving.year)+"-"+ str(moving.month)]=0
if moving.weekday() < 5:
d[str(moving.year)+"-"+ str(moving.month)] += 1
year = moving.year
month = moving.month
moving += datetime.timedelta(days=1)
print(d)
df_dow = pd.DataFrame(d, columns=['Month', 'DateValue'])
df_dow.Month = pd.to_datetime(df_dow.Month)
df_dow.sort('Month', inplace=True)
def holiday_adj(x):
if x['Month'].month==1:
x['DateValue'] -=1
return x['DateValue']
elif x['Month'].month==2:
x['DateValue'] -=1
return x['DateValue']
elif x['Month'].month==5:
x['DateValue'] -=1
return x['DateValue']
elif x['Month'].month==7:
x['DateValue'] -=1
return x['DateValue']
elif x['Month'].month==9:
x['DateValue'] -=1
return x['DateValue']
elif x['Month'].month==10:
x['DateValue'] -=1
return x['DateValue']
elif x['Month'].month==11:
x['DateValue'] -=3
return x['DateValue']
elif x['Month'].month==12:
x['DateValue'] -=2
return x['DateValue']
else:
return x['DateValue']
df_dow['days'] = df_dow.apply(holiday_adj, axis=1)
df_dow.set_index('Month', inplace=True)
df_dow.index.name = None
错误和输出:
{'1976-5': 21, '1978-10': 22, '1974-11': 21, '1981-9': 22, '1976-10': 21, '1977-10': 21, '1979-10': 23, '1981-11': 21, '1974-9': 21, '1981-8': 21, '1975-10': 23, '1979-9': 20, '1977-7': 21, '1974-6': 20, '1975-2': 20, '1977-8': 23, '1981-10': 22, '1978-2': 20, '1980-2': 21, '1978-6': 22, '1977-5': 22, '1982-3': 23, '1980-11': 20, '1980-3': 21, '1982-4': 22, '1980-4': 22, '1973-12': 21, '1976-6': 22, '1982-5': 21, '1977-2': 20, '1982-6': 22, '1978-8': 23, '1980-1': 23, '1980-9': 22, '1979-3': 22, '1973-3': 22, '1977-1': 21, '1976-9': 22, '1980-12': 23, '1974-5': 23, '1979-12': 21, '1981-6': 22, '1973-2': 20, '1973-4': 21, '1978-11': 22, '1981-3': 22, '1975-1': 23, '1978-9': 21, '1978-5': 23, '1973-10': 23, '1975-11': 20, '1973-1': 23, '1977-4': 21, '1973-5': 23, '1979-11': 22, '1974-3': 21, '1976-4': 22, '1973-11': 22, '1976-1': 22, '1981-7': 23, '1974-4': 22, '1975-12': 23, '1978-3': 23, '1976-7': 22, '1976-11': 22, '1976-12': 23, '1978-4': 20, '1979-2': 20, '1974-12': 22, '1975-6': 21, '1981-4': 22, '1982-2': 20, '1979-5': 23, '1973-8': 23, '1974-1': 23, '1975-7': 23, '1980-10': 23, '1973-9': 20, '1981-2': 20, '1979-4': 21, '1981-1': 22, '1976-3': 23, '1975-5': 22, '1977-6': 22, '1976-2': 20, '1974-7': 23, '1982-1': 21, '1973-6': 21, '1979-7': 22, '1980-6': 21, '1980-5': 22, '1977-11': 22, '1980-7': 23, '1979-1': 23, '1981-12': 23, '1979-8': 23, '1973-7': 22, '1975-8': 21, '1975-9': 22, '1974-2': 20, '1978-1': 22, '1977-3': 23, '1977-12': 22, '1974-10': 23, '1977-9': 22, '1979-6': 21, '1981-5': 21, '1975-4': 22, '1978-7': 21, '1976-8': 22, '1975-3': 21, '1978-12': 21, '1974-8': 22, '1980-8': 21}
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-50-f4b80a158a0d> in <module>()
30 print(d)
31
---> 32 df_dow = pd.DataFrame(d.items(), columns=['Month', 'DateValue'])
33 df_dow.Month = pd.to_datetime(df_dow.Month)
34 df_dow.sort('Month', inplace=True)
c:\python35\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
402 dtype=values.dtype, copy=False)
403 else:
--> 404 raise ValueError('DataFrame constructor not properly called!')
405
406 NDFrame.__init__(self, mgr, fastpath=True)
ValueError: DataFrame constructor not properly called!
请告诉我是否错过了什么?
答案 0 :(得分:2)
我认为需要通过DaatFrame
和key
s创建value
:
df = pd.DataFrame({'Month': list(d.keys()), 'DateValue':list(d.values())})
或通过DataFrame.from_dict
与reset_index
一起使用:
df = pd.DataFrame.from_dict(d, orient='index').reset_index()
df.columns=['Month', 'DateValue']
print (df.head())
Month DateValue
0 1976-5 21
1 1978-10 22
2 1974-11 21
3 1981-9 22
4 1976-10 21
答案 1 :(得分:1)
您还可以创建模板DataFrame,然后只需说一遍即可分配新列:
DeploymentException