pandas 将 float64 列转换为日期时间熊猫

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

Convert float64 column to datetime pandas

pythonpython-3.xpandasdatetimedataframe

提问by JohnAndrews

I have the following pandas DataFramecolumn dfA['TradeDate']:

我有以下pandas DataFrame专栏dfA['TradeDate']

0     20100329.0
1     20100328.0
2     20100329.0
...

and I wish to transform it to a datetime.

我希望将其转换为日期时间。

Based on another tread on SO, I convert it first to a string and then apply the strptimefunction.

基于 SO 上的另一个步骤,我首先将其转换为字符串,然后应用该strptime函数。

dfA['TradeDate'] = datetime.datetime.strptime( dfA['TradeDate'].astype('int').to_string() ,'%Y%m%d')

However this returns the error that my format is incorrect (ValueError).

但是,这会返回我的格式不正确的错误 ( ValueError)。

An issue that I spotted is that the column is not properly to string, but to an object.

我发现的一个问题是该列不能正确串接,而是串接一个对象。

When I try:

当我尝试:

dfA['TradeDate'] = datetime.datetime.strptime( dfA['TradeDate'].astype(int).astype(str),'%Y%m%d')

It returns: must be a Str and not Series.

它返回:必须是 Str 而不是系列。

回答by jezrael

You can use:

您可以使用:

df['TradeDate'] = pd.to_datetime(df['TradeDate'], format='%Y%m%d.0')
print (df)
   TradeDate
0 2010-03-29
1 2010-03-28
2 2010-03-29

But if some bad values, add errors='coerce'for replace them to NaT

但是如果一些不好的值,添加errors='coerce'替换它们NaT

print (df)
    TradeDate
0  20100329.0
1  20100328.0
2  20100329.0
3  20153030.0
4         yyy

df['TradeDate'] = pd.to_datetime(df['TradeDate'], format='%Y%m%d.0', errors='coerce')
print (df)
   TradeDate
0 2010-03-29
1 2010-03-28
2 2010-03-29
3        NaT
4        NaT

回答by languitar

You can use to_datetimewith a custom format on a string representation of the values:

您可以to_datetime在值的字符串表示形式上使用自定义格式:

import pandas as pd
pd.to_datetime(pd.Series([20100329.0, 20100328.0, 20100329.0]).astype(str), format='%Y%m%d.0')

回答by aman sohane

strptimefunction works on a single value, not on series. You need to apply that function to each element of the column

strptime函数适用于单个值,而不适用于系列。您需要将该函数应用于列的每个元素

try pandas.to_datetimemethod

尝试pandas.to_datetime方法

eg

例如

dfA = pandas.DataFrame({"TradeDate" : [20100329.0,20100328.0]})
pandas.to_datetime(dfA['TradeDate'], format = "%Y%m%d")

or

或者

dfA['TradeDate'].astype(int).astype(str)\ 
    .apply(lambda x:datetime.datetime.strptime(x,'%Y%m%d'))

回答by R Kiselev

In your first attempt you tried to convert it to string and then pass to strptime, which resulted in ValueError. This happens because dfA['TradeDate'].astype('int').to_string()creates a single string containing alldates as well as their row numbers. You can change this to

在您的第一次尝试中,您尝试将其转换为字符串,然后传递给strptime,这导致ValueError. 发生这种情况是因为dfA['TradeDate'].astype('int').to_string()创建了一个包含所有日期及其行号的字符串。您可以将其更改为

dates = dfA['TradeDate'].astype('int').to_string(index=False).split()
dates
[u'20100329.0', u'20100328.0', u'20100329.0']

to get a list of dates. Then use python list comprehensionto convert each element to datetime:

获取日期列表。然后使用python 列表推导将每个元素转换为datetime

dfA['TradeDate'] = [datetime.strptime(x, '%Y%m%d.0') for x in dates]