1
votes

Hope you can help me. I want to check if a date is valid or not. I've read that I can do this with datetime. My code is:

import pandas as pd
import datetime as dt

df = pd.read_csv("file.csv", encoding="latin1")

dfTest = pd.DataFrame()
dfTest['year'] = df.GEBURTSTAG.dt.year
dfTest['month'] = df.GEBURTSTAG.dt.month
dfTest['day'] = df.GEBURTSTAG.dt.day

dfTest['check'] = dt.datetime(dfTest.year, dfTest.month, dfTest.day)

If I run that code the following TypeError shows up:

cannot convert the series to class 'int'

I can't find the problem.

Thx

1
Does this answer your question? cannot convert the series to <class 'int'`> - Joshua
thx for your answer. I've already tried that but same TypError shows up. dtype in year, month and day is int64 - Chuck

1 Answers

0
votes

Use .astype(int)

dfTest['year'] = df.GEBURTSTAG.dt.year.astype(int)
dfTest['month'] = df.GEBURTSTAG.dt.month.astype(int)
dfTest['day'] = df.GEBURTSTAG.dt.day.astype(int)

To convert numpy.int64 to int:

dfTest = (pd.DataFrame()).to_dict()

.tolist() also works.