Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas 'astype' with date (or datetime)

This answer contains a very elegant way of setting all the types of your pandas columns in one line:

# convert column "a" to int64 dtype and "b" to complex type
df = df.astype({"a": int, "b": complex})

I am starting to think that that unfortunately has limited application and you will have to use various other methods of casting the column types sooner or later, over many lines. I tested 'category' and that worked, so it will take things which are actual python types like int or complex and then pandas terms in quotation marks like 'category'.

I have a column of dates which looks like this:

25.07.10
08.08.10
07.01.11

I had a look at this answer about casting date columns but none of them seem to fit into the elegant syntax above.

I tried:

from datetime import date
df = df.astype({"date": date})

but it gave an error:

TypeError: dtype '<class 'datetime.date'>' not understood

I also tried pd.Series.dt.date which also didn't work. Is it possible to cast all your columns including the date or datetime column in one line like this?

like image 621
cardamom Avatar asked Dec 06 '25 15:12

cardamom


1 Answers

This has been answered in the comments where it was noted that the following works:

df.astype({'date': 'datetime64[ns]'})

In addition, you can set the dtype when reading in the data:

pd.read_csv('path/to/file.csv', parse_dates=['date'])
like image 190
joelostblom Avatar answered Dec 08 '25 05:12

joelostblom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!