如何在熊猫中创建列日期

时间:2019-01-03 04:14:27

标签: python python-3.x pandas dataframe

我想向熊猫数据框添加一个列,该列是月末日期。基于此answer,我尝试了以下操作:

import numpy as np
import pandas as pd

dates = ['2014-06-02', '2014-06-03', '2014-06-04', '2014-06-05', '2014-06-06']
sp500_index = [1924.969971, 1924.23999, 1927.880005, 1940.459961, 1949.439941]
df_sp500 = pd.DataFrame({'Date' : dates, 'Close' : sp500_index})
sp500['Date'] = pd.to_datetime(sp500['Date'], format='%Y-%m-%d')
df_sp500['EOM'] = df_sp500['Date'].dt.ceil('M')  # breaks on this line
#df_sp500 = df_sp500[df_sp500['Date'] == df_sp500['EOM']]

df_sp500

但我收到此错误消息:

  

AttributeError:只能使用具有datetimelike值的.dt访问器

我要添加此列的原因是使用它来过滤除EOM日期以外的所有内容,如注释行中所示。

3 个答案:

答案 0 :(得分:3)

import numpy as np
import pandas as pd
from pandas.tseries.offsets import MonthEnd


dates = ['2014-06-02', '2014-06-03', '2014-06-04', '2014-06-05', '2014-06-06']
sp500_index = [1924.969971, 1924.23999, 1927.880005, 1940.459961, 1949.439941]
df_sp500 = pd.DataFrame({'Date' : dates, 'Close' : sp500_index})
df_sp500['EOM'] = pd.to_datetime(df_sp500['Date'], format='%Y-%m-%d')+ MonthEnd(0)
#df_sp500['EOM']=df_sp500['EOM'].dt.day #add this if you want only day

答案 1 :(得分:1)

pandas.Series.is_month_end已内置到datetime中。代替计算新列,只需使用以下子集即可:

df_sp500[df_sp500.Date.dt.is_month_end]

输入数据

dates = ['2014-06-02', '2014-06-03', '2014-06-04', '2014-06-05', '2014-06-06']
sp500_index = [1924.969971, 1924.23999, 1927.880005, 1940.459961, 1949.439941]

df_sp500 = pd.DataFrame({'Date' : dates, 'Close' : sp500_index})
df_sp500['Date'] = pd.to_datetime(df_sp500['Date'], format='%Y-%m-%d')

答案 2 :(得分:0)

基于文档

  

要建立索引的频率级别。必须为固定频率   例如“ S”(秒)而不是“ ME”(月底)

因此,我们可能会根据您的情况使用MonthBegin

df_sp500['Date']- pd.offsets.MonthBegin(1) #pd.offsets.MonthEnd(1)
0   2014-06-01
1   2014-06-01
2   2014-06-01
3   2014-06-01
4   2014-06-01
Name: Date, dtype: datetime64[ns]