Python 从日期时间 <[M8] 在 Pandas 中删除时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24786209/
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
Dropping time from datetime <[M8] in Pandas
提问by user2926266
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?
如果我只想要年月和日,我该如何放弃琐碎的时间?
采纳答案by Andy Hayden
The quickest way is to use DatetimeIndex's normalize (you first need to make the column a DatetimeIndex):
最快的方法是使用 DatetimeIndex 的 normalize(您首先需要将该列设为 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
DatetimeIndex also has some other useful attributes, e.g. .year, .month, .day.
DatetimeIndex 还有一些其他有用的属性,例如 .year、.month、.day。
From 0.15 they'll be a dt attribute, so you can access this (and other methods) with:
从 0.15 开始,它们将成为 dt 属性,因此您可以使用以下方法访问此(和其他方法):
df.t.dt.normalize()
# equivalent to
pd.DatetimeIndex(df.t).normalize()
回答by Alexander Stefánsson
Another Possibility is using str.split
另一种可能性是使用 str.split
df['Date'] = df['Date'].str.split(' ',expand=True)[0]
This should split the 'Date' column into two columns marked 0 and 1. Using the whitespace in between the date and time as the split indicator.
这应该将“日期”列拆分为标记为 0 和 1 的两列。使用日期和时间之间的空格作为拆分指示符。
Column 0 of the returned dataframe then includes the date, and column 1 includes the time. Then it sets the 'Date' column of your original dataframe to column [0] which should be just the date.
返回的数据帧的第 0 列包含日期,第 1 列包含时间。然后它将原始数据框的“日期”列设置为列 [0],这应该只是日期。
回答by mccandar
Another option
另外一个选项
df['my_date_column'].dt.date
Would give
会给
0 2019-06-15
1 2019-06-15
2 2019-06-15
3 2019-06-15
4 2019-06-15