Outcast
Outcast

Reputation: 5117

Convert string of date to datetime

I am doing the following:

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 is an object type column which containts dates (e.g. 2018-01-03) and I want to convert it a datetime object.

I take this error:

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'>

How can I fix this?

Upvotes: 0

Views: 95

Answers (1)

Koukou
Koukou

Reputation: 187

Just replace pd.datetime() with pd.to_datetime().

Upvotes: 1

Related Questions