Error by creating an additional column in my dataframe

Hey community,

first i had a csv file with data and then I decided to make a db connection to get the data directly from the database and it’s working finde.

when I used the csv file to fill my dataframe I added two additional columns with
df[‘Jahr’] = pd.DatetimeIndex(df[‘Datum’]).year
df[‘Monat’] = pd.DatetimeIndex(df[‘Datum’]).month

and its working.

But after I decided to fill my df with a db connection I’m getting errors when I try to create a year and month column with
df[‘Jahr’] = pd.DatetimeIndex(df[‘Datum’]).year
df[‘Monat’] = pd.DatetimeIndex(df[‘Datum’]).month

Error:
Traceback (most recent call last):
File “C:\Users\User\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\core\arrays\datetimes.py”, line 2211, in objects_to_datetime64ns
values, tz_parsed = conversion.datetime_to_datetime64(data.ravel(“K”))
File “pandas_libs\tslibs\conversion.pyx”, line 360, in pandas._libs.tslibs.conversion.datetime_to_datetime64
TypeError: Unrecognized value type: <class ‘datetime.date’>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\User\Desktop\DashProjekt\Jonen\ProjektBA\AlchemyTestSelectWithBar.py”, line 41, in
df[‘Jahr’] = pd.DatetimeIndex(df[‘Datum’]).year
File “C:\Users\User\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\core\indexes\datetimes.py”, line 327, in new
dtarr = DatetimeArray._from_sequence_not_strict(
File “C:\Users\User\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\core\arrays\datetimes.py”, line 358, in _from_sequence_not_strict
subarr, tz, inferred_freq = _sequence_to_dt64ns(
File “C:\Users\User\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\core\arrays\datetimes.py”, line 2082, in _sequence_to_dt64ns
data, inferred_tz = objects_to_datetime64ns(
File “C:\Users\User\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\core\arrays\datetimes.py”, line 2217, in objects_to_datetime64ns
raise err
File “C:\Users\User\AppData\Local\Programs\Python\Python39-32\lib\site-packages\pandas\core\arrays\datetimes.py”, line 2199, in objects_to_datetime64ns
result, tz_parsed = tslib.array_to_datetime(
File “pandas_libs\tslib.pyx”, line 381, in pandas._libs.tslib.array_to_datetime
File “pandas_libs\tslib.pyx”, line 608, in pandas._libs.tslib.array_to_datetime
File “pandas_libs\tslib.pyx”, line 604, in pandas._libs.tslib.array_to_datetime
File “pandas_libs\tslib.pyx”, line 481, in pandas._libs.tslib.array_to_datetime
File “pandas_libs\tslibs\np_datetime.pyx”, line 120, in pandas._libs.tslibs.np_datetime.check_dts_bounds
pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 3622-09-25 00:00:00

I hope somebody can help me.

Thanks in advance.

Greetings Mae

1 Like

Hi,

Given the error you provided, it looks like pandas is not able to parse the dates into timestamp correctly:

pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 3622-09-25 00:00:00

It can be many things, including bad entries in the db or in the parser, but for sure not Dash related.

Hope this helps!

Hello jlfsjunior,

i found the error it was a row with the date 3622-09-25 which made the error. When I adoid this row everything is fine.

Greetings Mae