将日期字符串转换为日期时间

时间:2019-03-14 14:47:44

标签: pandas datetime

我正在执行以下操作:

import pandas as pd
from datetime import date

data = pd.read_csv('C:/Users/User/Desktop/data.txt', keep_default_na=True, sep='\t', na_values='?', nrows=10000)


data['Start_date'] = data.groupby(['Stock'])['Dates'].transform('min')


data['Start_date'] = pd.datetime(data['Start_date'])

Start_date是一个对象类型列,其中包含日期(例如2018年1月3日),我想将其转换为日期时间对象。

我遇到此错误:

Traceback (most recent call last):
  File "C:/Users/User/PycharmProjects/OGP_project/trial.py", line 15, in <module>
    data['Start_date'] = pd.datetime(data['Start_date'])
  File "C:\Users\User\PycharmProjects\project\venv\lib\site-packages\pandas\core\series.py", line 118, in wrapper
    "{0}".format(str(converter)))
TypeError: cannot convert the series to <class 'int'>

我该如何解决?

1 个答案:

答案 0 :(得分:1)

只需将pd.datetime()替换为pd.to_datetime()