pandas 从日期时间对象中提取日期和月份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51603690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:51:53  来源:igfitidea点击:

Extract day and month from a datetime object

pythonpython-3.xpandasdatetime

提问by Abhi

I have a column with dates in string format '2017-01-01'. Is there a way to extract day and month from it using pandas?

我有一个字符串格式的日期列'2017-01-01'。有没有办法使用Pandas从中提取日期和月份?

I have converted the column to datetime dtypebut haven't figured out the later part:

我已将列转换为datetime dtype但尚未弄清楚后面的部分:

df['Date'] =  pd.to_datetime(df['Date'], format='%Y-%m-%d')

df.dtypes: 
Date        datetime64[ns]

print(df)

         Date
0   2017-05-11
1   2017-05-12
2   2017-05-13 

回答by YOBEN_S

With dt.dayand dt.month--- Series.dt

随着dt.daydt.month---Series.dt

df = pd.DataFrame({'date':pd.date_range(start='2017-01-01',periods=5)})
df.date.dt.month
Out[164]: 
0    1
1    1
2    1
3    1
4    1
Name: date, dtype: int64

df.date.dt.day
Out[165]: 
0    1
1    2
2    3
3    4
4    5
Name: date, dtype: int64

Also can do with dt.strftime

也可以做 dt.strftime

df.date.dt.strftime('%m')
Out[166]: 
0    01
1    01
2    01
3    01
4    01
Name: date, dtype: object

回答by aydow

Use dtto get the datetimeattributes of the column.

使用dt来获取datetime列的属性。

In [60]: df = pd.DataFrame({'date': [datetime.datetime(2018,1,1),datetime.datetime(2018,1,2),datetime.datetime(2018,1,3),]})

In [61]: df
Out[61]:
        date
0 2018-01-01
1 2018-01-02
2 2018-01-03

In [63]: df['day'] = df.date.dt.day

In [64]: df['month'] = df.date.dt.month

In [65]: df
Out[65]:
        date  day  month
0 2018-01-01    1      1
1 2018-01-02    2      1
2 2018-01-03    3      1

Timing the methods provided:

定时提供的方法:

Using apply:

使用apply

In [217]: %timeit(df['date'].apply(lambda d: d.day))
The slowest run took 33.66 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 210 μs per loop

Using dt.date:

使用dt.date

In [218]: %timeit(df.date.dt.day)
10000 loops, best of 3: 127 μs per loop

Using dt.strftime:

使用dt.strftime

In [219]: %timeit(df.date.dt.strftime('%d'))
The slowest run took 40.92 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 284 μs per loop

We can see that dt.dayis the fastest

我们可以看到这dt.day是最快的

回答by Babak

A simple form:

一个简单的表格:

df['MM-DD'] = df['date'].dt.strftime('%m-%d')

回答by Peybae

This should do it:

这应该这样做:

df['day'] = df['Date'].apply(lambda r:r.day)
df['month'] = df['Date'].apply(lambda r:r.month)