从Panda的datetime< [M8]中删除时间 [英] Dropping time from datetime <[M8] in Pandas
本文介绍了从Panda的datetime< [M8]中删除时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我的数据框中有一个'日期'列,日期格式如下
So I have a 'Date' column in my data frame where the dates have the format like this
0 1998-08-26 04:00:00
如果我只想要年份月份和日子我该如何下降简单的小时?
If I only want the Year month and day how do I drop the trivial hour?
推荐答案
最快的方法是使用DatetimeIndex的normalize(你首先需要使列为DatetimeIndex):
The quickest way is to use DatetimeIndex's normalize (you first need to make the column a DatetimeIndex):
In [11]: df = pd.DataFrame({"t": pd.date_range('2014-01-01', periods=5, freq='H')})
In [12]: df
Out[12]:
t
0 2014-01-01 00:00:00
1 2014-01-01 01:00:00
2 2014-01-01 02:00:00
3 2014-01-01 03:00:00
4 2014-01-01 04:00:00
In [13]: pd.DatetimeIndex(df.t).normalize()
Out[13]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01, ..., 2014-01-01]
Length: 5, Freq: None, Timezone: None
In [14]: df['date'] = pd.DatetimeIndex(df.t).normalize()
In [15]: df
Out[15]:
t date
0 2014-01-01 00:00:00 2014-01-01
1 2014-01-01 01:00:00 2014-01-01
2 2014-01-01 02:00:00 2014-01-01
3 2014-01-01 03:00:00 2014-01-01
4 2014-01-01 04:00:00 2014-01-01
。年,.month,.day。
DatetimeIndex also has some other useful attributes, e.g. .year, .month, .day.
从0.15他们将是一个dt属性,所以您可以使用以下方式访问此(和其他方法):
From 0.15 they'll be a dt attribute, so you can access this (and other methods) with:
df.t.dt.normalize()
# equivalent to
pd.DatetimeIndex(df.t).normalize()
这篇关于从Panda的datetime< [M8]中删除时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文