Python 如何将float列转换为datetime?
年份:int64 月份:64 日期:64 小时:对象 分钟:对象 我正试图通过合并这些列来确定日期,但我一直得到一个错误。我不知道我该怎么办 这是我的代码和错误Python 如何将float列转换为datetime?,python,Python,年份:int64 月份:64 日期:64 小时:对象 分钟:对象 我正试图通过合并这些列来确定日期,但我一直得到一个错误。我不知道我该怎么办 这是我的代码和错误 r_datetime = pd.to_datetime( str(int(df['year'])) \ + '-' + str(int(df['month'])) \ + '-' + str(int(df['day'])) \ + ' ' +
r_datetime = pd.to_datetime(
str(int(df['year'])) \
+ '-' + str(int(df['month'])) \
+ '-' + str(int(df['day'])) \
+ ' ' + str(int(df['hour'])) \
+ ':' + str(int(df['minute']))
)
TypeError: cannot convert the series to <class 'int'>
r\u datetime=pd.to\u datetime(
str(int(df['年]))\
+'-'+str(int(df['month']))\
+'-'+str(int(df['day']))\
+''+str(int(df['hour']))\
+“:”+str(int(df['minute']))
)
TypeError:无法将序列转换为
astype(str)也出现了错误。
我不知道该怎么办
感谢您的帮助。熊猫数据框提供了更改列值数据类型的自由。我们可以将它们从整数更改为浮点类型、从整数更改为日期时间、从字符串更改为整数、从浮点更改为日期时间等。要将浮点转换为日期时间,我们使用
pandas.to_Datetime()
函数,并使用以下语法:
Syntax: pandas.to_datetime(arg, errors=’raise’, dayfirst=False, yearfirst=False,
utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False,
origin=’unix’, cache=False)
使用pandas.to\u datetime()
熊猫数据框提供了更改列值数据类型的自由。我们可以将它们从整数更改为浮点类型、从整数更改为日期时间、从字符串更改为整数、从浮点更改为日期时间等。要将浮点转换为日期时间,我们使用
pandas.to_Datetime()
函数,并使用以下语法:
Syntax: pandas.to_datetime(arg, errors=’raise’, dayfirst=False, yearfirst=False,
utc=None, box=True, format=None, exact=True, unit=None, infer_datetime_format=False,
origin=’unix’, cache=False)
使用pandas.to\u datetime()
在中阅读更多内容您可以尝试以下方法:
r_datetime = pd.to_datetime({'year': df['year'],
'month': df['month'],
df['day'].name: df['day'], # if you don't want to type column names explicitly. But will have to type it in any case.
'hour': df['hour'],
'minutes': df['minutes']},
format="%y%M%d%h%m")
请阅读。中的更多信息,您可以尝试如下内容:
r_datetime = pd.to_datetime({'year': df['year'],
'month': df['month'],
df['day'].name: df['day'], # if you don't want to type column names explicitly. But will have to type it in any case.
'hour': df['hour'],
'minutes': df['minutes']},
format="%y%M%d%h%m")
阅读。您好,很有意思,也许可以使用
pd.to_datetime(df['col',格式='%y%m%d')
阅读:。您可以使用类型为string的datetime列填充数据帧,然后将此列强制转换为datetimehi,有趣的是,可以使用pd.to_datetime(df['col',格式='%y%m%d')
阅读:。您可以使用类型为string的datetime列填充dataframe,然后将该列强制转换为datetime