Python 从熊猫的日期和时间变量中删除时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29310116/
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-08-19 04:22:20  来源:igfitidea点击:

removing time from date&time variable in pandas?

pythonpandas

提问by surendra

i have a variable consisting of 300k records with dates and the date look like
2015-02-21 12:08:51
from that date i want to remove time

我有一个变量由 300k 条记录组成,日期和日期看起来像
2015-02-21 12:08:51
从那个日期开始我想删除时间

type of date variable is pandas.core.series.series

日期变量的类型是 pandas.core.series.series

This is the way i tried

这是我尝试的方式

from datetime import datetime,date
date_str = textdata['vfreceiveddate']  
format_string = "%Y-%m-%d"
then = datetime.strftime(date_str,format_string)   

some Random ERROR

一些随机错误

In the above code textdata is my datasetname and vfreceived date is a variable consisting of dates
How can i write the code to remove the time from the datetime.

在上面的代码中,textdata 是我的数据集名称,而 vfreceived 日期是一个由日期组成的变量
我如何编写代码以从日期时间中删除时间。

采纳答案by EdChum

Assuming all your datetime strings are in a similar format then just convert them to datetime using to_datetimeand then call the dt.dateattribute to get just the date portion:

假设您所有的日期时间字符串都采用类似的格式,那么只需使用将它们转换为日期时间to_datetime,然后调用该dt.date属性即可获取日期部分:

In [37]:

df = pd.DataFrame({'date':['2015-02-21 12:08:51']})
df
Out[37]:
                  date
0  2015-02-21 12:08:51
In [39]:

df['date'] = pd.to_datetime(df['date']).dt.date
df
Out[39]:
         date
0  2015-02-21

EDIT

编辑

If you just want to change the display and not the dtype then you can call dt.normalize:

如果您只想更改显示而不是 dtype,则可以调用dt.normalize

In[10]:
df['date'] = pd.to_datetime(df['date']).dt.normalize()
df

Out[10]: 
        date
0 2015-02-21

You can see that the dtype remains as datetime:

您可以看到 dtype 保持为datetime

In[11]:
df.dtypes

Out[11]: 
date    datetime64[ns]
dtype: object

回答by Alex Martelli

You're calling datetime.datetime.strftime, which requires as its first argument a datetime.datetimeinstance, because it's an unbound method; but you're passing it a string instead of a datetime instance, whence the obvious error.

您正在调用datetime.datetime.strftime,它需要一个datetime.datetime实例作为它的第一个参数,因为它是一个未绑定的方法;但是您传递给它的是一个字符串而不是日期时间实例,因此出现了明显的错误。

You can work purely at a string level if that's the result you want; with the data you give as an example, date_str.split()[0]for example would be exactly the 2015-02-21string you appear to require.

如果这是您想要的结果,您可以纯粹在字符串级别工作;例如,date_str.split()[0]以您提供的数据为例,这正是2015-02-21您似乎需要的字符串。

Or, you canuse datetime, but then you need to parsethe string first, not formatit -- hence, strptime, notstrftime:

或者,您可以使用datetime,但随后您需要先解析字符串,而不是对其进行格式化——因此,是 str p时间,而不是str f时间:

dt = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
date = dt.date()

if it's a datetime.dateobject you want (but if all you want is the string form of the date, such an approach might be "overkill":-).

如果它是datetime.date你想要的对象(但如果你想要的只是日期的字符串形式,这种方法可能是“矫枉过正”:-)。