Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell pandas to parse a particular column as a datetime object, but not make it an index?

I have a csv file where one of the columns is a date/time string. How do I parse it correctly with pandas? I don't want to make that column the index. Thanks!

Uri

like image 382
Uri Laserson Avatar asked Mar 15 '12 15:03

Uri Laserson


People also ask

Which pandas function converts column of datetime string to a column of datetime object?

Use astype() function to convert the string column to datetime data type in pandas DataFrame. The data type of the DateTime isdatetime64[ns] ; should be given as the parameter. Yields same output as above.

What is the method in pandas to change values into datetime data type?

In Pandas, you can convert a column (string/object or integer type) to datetime using the to_datetime() and astype() methods.

How do I parse a date in pandas?

For non-standard datetime parsing, use pd.to_datetime after pd.read_csv . To parse an index or column with a mixture of timezones, specify date_parser to be a partially-applied pandas.to_datetime() with utc=True . See Parsing a CSV with mixed timezones for more.

How do I select a specific column in pandas?

To select a single column, use square brackets [] with the column name of the column of interest.


1 Answers

Pass dateutil.parser.parse (or another datetime conversion function) in the converters argument to read_csv

like image 129
Wes McKinney Avatar answered Sep 30 '22 06:09

Wes McKinney