python pandas 从日期时间中提取年份:df['year'] = df['date'].year 不起作用
我通过 read_csv
导入了一个数据框,但由于某种原因无法从系列 df['date']
中提取年份或月份,尝试这给出 AttributeError: 'Series' 对象没有属性 'year'
:
I import a dataframe via read_csv
, but for some reason can't extract the year or month from the series df['date']
, trying that gives AttributeError: 'Series' object has no attribute 'year'
:
date Count
6/30/2010 525
7/30/2010 136
8/31/2010 125
9/30/2010 84
10/29/2010 4469
df = pd.read_csv('sample_data.csv', parse_dates=True)
df['date'] = pd.to_datetime(df['date'])
df['year'] = df['date'].year
df['month'] = df['date'].month
更新:当我在我的 Pandas 0.14.1 版本上尝试使用 df['date'].dt
的解决方案时,我得到AttributeError: 'Series' object has no attribute 'dt'":>
UPDATE:
and when I try solutions with df['date'].dt
on my pandas version 0.14.1, I get "AttributeError: 'Series' object has no attribute 'dt' ":
df = pd.read_csv('sample_data.csv',parse_dates=True)
df['date'] = pd.to_datetime(df['date'])
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
对于这个看似重复的问题,我很抱歉 - 我希望答案会让我觉得自己像个傻瓜...但我在使用 SO 上类似问题的答案时没有任何运气.
Sorry for this question that seems repetitive - I expect the answer will make me feel like a bonehead... but I have not had any luck using answers to the similar questions on SO.
跟进:我似乎无法在我的 Anaconda 环境中将我的 Pandas 0.14.1 更新到更新的版本,下面的每次尝试都会产生一个无效的语法错误.我使用的是 Python 3.4.1 64 位.
FOLLOWUP: I can't seem to update my pandas 0.14.1 to a newer release in my Anaconda environment, each of the attempts below generates an invalid syntax error. I'm using Python 3.4.1 64bit.
conda update pandas
conda install pandas==0.15.2
conda install -f pandas
有什么想法吗?
如果您运行的是最新版本的 Pandas,那么您可以使用 datetime 属性 dt
访问日期时间组件:
If you're running a recent-ish version of pandas then you can use the datetime attribute dt
to access the datetime components:
In [6]:
df['date'] = pd.to_datetime(df['date'])
df['year'], df['month'] = df['date'].dt.year, df['date'].dt.month
df
Out[6]:
date Count year month
0 2010-06-30 525 2010 6
1 2010-07-30 136 2010 7
2 2010-08-31 125 2010 8
3 2010-09-30 84 2010 9
4 2010-10-29 4469 2010 10
编辑
看起来您运行的是旧版本的 Pandas,在这种情况下,以下方法可行:
It looks like you're running an older version of pandas in which case the following would work:
In [18]:
df['date'] = pd.to_datetime(df['date'])
df['year'], df['month'] = df['date'].apply(lambda x: x.year), df['date'].apply(lambda x: x.month)
df
Out[18]:
date Count year month
0 2010-06-30 525 2010 6
1 2010-07-30 136 2010 7
2 2010-08-31 125 2010 8
3 2010-09-30 84 2010 9
4 2010-10-29 4469 2010 10
关于为什么它没有将其解析为 read_csv
中的日期时间,您需要传递列的序数位置 ([0]
) 因为当 True
它尝试解析列 [1,2,3]
参见 文档
Regarding why it didn't parse this into a datetime in read_csv
you need to pass the ordinal position of your column ([0]
) because when True
it tries to parse columns [1,2,3]
see the docs
In [20]:
t="""date Count
6/30/2010 525
7/30/2010 136
8/31/2010 125
9/30/2010 84
10/29/2010 4469"""
df = pd.read_csv(io.StringIO(t), sep='s+', parse_dates=[0])
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 2 columns):
date 5 non-null datetime64[ns]
Count 5 non-null int64
dtypes: datetime64[ns](1), int64(1)
memory usage: 120.0 bytes
因此,如果您将参数 parse_dates=[0]
传递给 read_csv
,则不需要在日期"上调用 to_datetime
' 加载后的列.
So if you pass param parse_dates=[0]
to read_csv
there shouldn't be any need to call to_datetime
on the 'date' column after loading.