0

My csv file has a column with date times in it that look like this :

"2021-01-24 20:10:58"

and I've written the following :

import pandas as pd

data = pd.read_csv(r'C:\Users\user\Desktop\foo\data.csv', index_col=1, parse_dates=True, infer_datetime_format=True)    
print(data.dtypes)

which shows basically all of my columns as 'object' type. Can someone help me understand what I'm missing?

1 Answer 1

1

You should pass a list of column names or indices, e.g. parse_dates=["col1", "col2"] or parse_dates=[1, 2]. If you pass parse_dates=True, only the index column will be parsed.

Per the docs:

The behavior is as follows:

boolean. If True -> try parsing the index.

list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.

list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.

dict, e.g. {‘foo’ : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’

Sign up to request clarification or add additional context in comments.

4 Comments

OK this seems to solve the issue only when I remove index_col=1. Any idea why that would be? Thank you and apologies for not reading the documentation ahead of time.
Infact calling setIndex does the same thing . I wonder if setting a column as an index removes it from the dtypes?
@AmirAfghani bcz it act as the index of the dataframe so when you write df.dtypes the dtypes attribute returns a Series with the data type of each column so dtypes won't work on indexes
Correct, setting a column as an index removes it from df.dtypes. Indices are treated differently than columns. You can access the dtype of the index with df.index.dtype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.