Ashish
Ashish

Reputation: 125

how to read data from csv as date format in python pandas

I have data in following format

Month        Country BA Total
11/1/2018     CN     3   10 

after reading Month comes as object though I want in date format, I tried to convert it in date time format using

hs = pd.read_csv('history.csv',parse_dates=['Month'])  #this is not solving the issue either

hs['Month'] = pd.to_datetime(hs['Month']) #this throws error

Please suggest me how to read it as date or convert it to date format

Upvotes: 0

Views: 8337

Answers (5)

Ashish
Ashish

Reputation: 125

thanks a lot to all of the suggestions, I tried last one and it worked, as I am running short of time, could not try other suggestions, will definitely try once I get time.

Upvotes: -1

Mark Domino
Mark Domino

Reputation: 25

Since pandas represents timestamps in nanosecond resolution, the timespan that can be represented using a 64-bit integer is limited to approximately 584 years Workaround:

This will force the dates which are outside the bounds to NaT

pd.to_datetime(date_col_to_force, errors = 'coerce')

Upvotes: 1

Pierre D
Pierre D

Reputation: 26301

If the dates in the file are less than pristine, it is often a good idea to load the file without parse_dates, then use pd.to_datetime() which has better control, including format and how to deal with errors ('raise', 'coerce', or 'ignore').

Upvotes: 0

I'mahdi
I'mahdi

Reputation: 24069

try one of this two line, maybe don't get error, your error maybe base on day is first or month is first:

df['sale_date'] = pd.to_datetime(df['sale_date'], format='%m/%d/%y')
# or
df['sale_date'] = pd.to_datetime(df['sale_date'], dayfirst=False)

OR

df['sale_date'] = pd.to_datetime(df['sale_date'], format='%d/%m/%y')
# or
df['sale_date'] = pd.to_datetime(df['sale_date'], dayfirst=True)

Upvotes: 2

Akas Antony
Akas Antony

Reputation: 749

Try this

from datetime import datetime
dateparse = lambda x: datetime.strptime(x, '%m/%d/%Y')

df = pd.read_csv('history.csv', parse_dates=['Month'], date_parser=dateparse)

Upvotes: 1

Related Questions