Home > Software engineering >  How to parse Date and Timestamp columns respectively with Pandas.read_csv()?
How to parse Date and Timestamp columns respectively with Pandas.read_csv()?

Time:10-11

I'm using Pandas-1.1.5, if an upgrading can resolve my problem, I can do it.

I know how to parse a column into Timestamp type, if there is only one time-liked column in a csv file. i.e like this:

df = pd.read_csv('try-timestamp.csv', parse_dates=['tick_time'], date_parser=pd.Timestamp)
print(type(df['tick_time'][0]))

But now I have two time-liked columns, one for Date type, and the other is Timestamp:

sample_day,tick_time,data
2022-09-30,22/09/30 09:15:00.446675655,0.273437500
2022-09-30,22/09/30 09:15:00.951579650,0.189697266

I want the first column to be parsed into pd.Date type, and the second to pd.Timestamp.

How should I specify parsers for every columns respectively?

Additionally, how to specify a time zone info for them? They are all in a same time zone.

CodePudding user response:

You can simply give a list of columns to parse_dates like this:

df = pd.read_csv('try-timestamp.csv', parse_dates=['tick_time', 'sample_day'], date_parser=pd.Timestamp)

This will parse both columns as timestamps; there is no "pd.Date" type.

Then I would use pd.to_datetime to specify the timezone, as shown here

  • Related