Home > database >  Converting dataframe column to datetime result to TypeError: Unrecognized value type: <class 
Converting dataframe column to datetime result to TypeError: Unrecognized value type: <class 

Time:09-21

I am trying to extract date and time from one column of data frame. The name of the data frame is df_BA_new and the corresponding column is 1. Below you can see the column 1

0         200604281300
1         200604281330
2         200604281335
3         200604281350
4         200604281355
    
392395    202209161320
392396    202209161325
392397    202209161330
392398    202209161335
392399    202209161340
Name: 1, Length: 392400, dtype: object

I try to convert data on this column to date time using the code shown below

df_BA_new[1] = pd.to_datetime(df_BA_new[1],format="%Y%m%d%H%M")

However, I take the following error

    values, tz = conversion.datetime_to_datetime64(arg)
  File "pandas/_libs/tslibs/conversion.pyx", line 360, in pandas._libs.tslibs.conversion.datetime_to_datetime64
TypeError: Unrecognized value type: <class 'int'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/nurhanseyahi/opt/anaconda3/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/Users/nurhanseyahi/Library/CloudStorage/OneDrive-Personal/Trading/Phase 2/RD/Automation/Back adjuster/back_adjust.py", line 74, in select_BA
    df_BA_new[1] = pd.to_datetime(df_BA_new[1],format="%Y%m%d%H%M")
  File "/Users/nurhanseyahi/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 1051, in to_datetime
    values = convert_listlike(arg._values, format)
  File "/Users/nurhanseyahi/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 394, in _convert_listlike_datetimes
    res = _to_datetime_with_format(
  File "/Users/nurhanseyahi/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 514, in _to_datetime_with_format
    raise err
  File "/Users/nurhanseyahi/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 501, in _to_datetime_with_format
    res = _array_strptime_with_fallback(
  File "/Users/nurhanseyahi/opt/anaconda3/lib/python3.9/site-packages/pandas/core/tools/datetimes.py", line 437, in _array_strptime_with_fallback
    result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors)
  File "pandas/_libs/tslibs/strptime.pyx", line 156, in pandas._libs.tslibs.strptime.array_strptime
ValueError: unconverted data remains: 30

Thank you in advance

CodePudding user response:

This example might help;

import pandas as pd
df = pd.DataFrame({'date': [200604281300, 200604281330, 200604281335]}) # example dataframe
df['date'] = pd.to_datetime(df['date'],format='%Y%m%d%H%M')
df

Output

0   2006-04-28 13:00:00
1   2006-04-28 13:30:00
2   2006-04-28 13:35:00
Name: date, dtype: datetime64[ns]

CodePudding user response:

You almost there You need to convert type object to int64. It throwing error because you trying to give a object dtype as a input to pd. it must need int as input. For int too long we are uisng int64.

df_BA_new[1]=df_BA_new[1].astype(str).astype('int64')

df_BA_new[1] = pd.to_datetime(df_BA_new[1],format="%Y%m%d%H%M")

CodePudding user response:

I don't know why, but adding errors='coerce' argument solved the problem. There was no missing data on the column that is why I did not understand what was the problem.

df_BA_new[1] =pd.to_datetime(df_BA_new[1],errors='coerce',format="%Y%m%d%H%M")

  • Related