How to use pandas read csv

Question:

I have a dataframe where all the column names are dates. When I use .read_csv(), pandas reads these column names as strings. Is there a way to specify that I want the column names to be datetime objects.

Ideally I need this to be done as part of the .read_csv() call, rather than an additional line afterwards.

For example, the csv file looks something like

df = pd.DataFrame({'2022-10-25': [0, 1, 1], '2022-10-26': [1, 1, 0]})

and when I call .read_csv(), I want the column headers to be datetime objects, rather than strings.

Asked By: user112495

||

Answers:

Create the dummy dataset:

df = pd.DataFrame({'2022-10-25': [0, 1, 1], '2022-10-26': [1, 1, 0]})

Check the initial columns index:

df.columns

Output:

enter image description here

You can run the following code just after the .read_csv()

from datetime import datetime
datetime_columns = [datetime.strptime(col, '%Y-%m-%d') for col in df.columns]
df.columns = pd.Index(datetime_columns, dtype='datetime64[ns]')

Finally we check the new index:

df.columns

Output:

enter image description here

Answered By: ggeop

either you define the whole column as a datetime or as an integer… there is no way you can define the header as a datetime and the values as a different thing.

On the other hand you cannot define your columns as datetime, because the values are just integers… I think you are approaching your problem in a wrong way.

Answered By: Miguel Gonzalez
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.