Python 从熊猫数据框中的日期时间中删除时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45858155/
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
Removing the timestamp from a datetime in pandas dataframe
提问by DGMS89
Scenario:I have a dataframe with multiple columns retrieved from excel worksheets. Some of these columns are dates: some have just the date (yyyy:mm:dd) and some have date and timestamp (yyyy:mm:dd 00.00.000000).
场景:我有一个数据框,其中包含从 Excel 工作表中检索的多列。其中一些列是日期:一些列只有日期 (yyyy:mm:dd),一些列包含日期和时间戳 (yyyy:mm:dd 00.00.000000)。
Question:How can I remove the time stamp from the dates when they are not the index of my dataframe?
问题:当日期不是我的数据帧的索引时,如何从日期中删除时间戳?
What I already tried:From other posts here in SO (working with dates in pandas - remove unseen characters in datetime and convert to stringand How to strip a pandas datetime of date, hours and seconds) I found:
我已经尝试过的:从 SO 中的其他帖子(在熊猫中使用日期 - 删除日期时间中看不见的字符并转换为字符串以及如何去除日期、小时和秒的熊猫日期时间)我发现:
pd.DatetimeIndex(dfST['timestamp']).date
and
和
strfitme (df['timestamp'].apply(lambda x: x.strftime('%Y-%m-%d'))
But I can't seem to find a way to use those directly to the wanted column when it is not the index of my dataframe.
但是当它不是我的数据框的索引时,我似乎无法找到一种方法将它们直接用于想要的列。
回答by Andrew L
You can do the following:
您可以执行以下操作:
dfST['timestamp'] = pd.to_datetime(dfST['timestamp'])
to_datetime()
will infer the formatting of the date column. You can also pass errors='coerce'
if the column contains non-date values.
to_datetime()
将推断日期列的格式。errors='coerce'
如果列包含非日期值,您也可以通过。
After completing the above, you'll be able to create a new column containing only date values:
完成上述操作后,您将能够创建一个仅包含日期值的新列:
dfST['new_date_column'] = dfST['timestamp'].dt.date