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 typedf = 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.1008.08.1007.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 datedf = 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?