Home > front end >  Issues with datetime and isinstance()
Issues with datetime and isinstance()

Time:09-21

I want to find if any variable in a dataset is a datetime. I'm having some serious problems with this seemingly simple task. In the dataset below, 'time' is transformed to datetime using pandas to_datetime(). It becomes a datetime64[ns] type, and, if I'm reading the documentation correct, to_datetime() should return a datetime.datetime class? However, my isinstance() below returns false.

Any help would be much appreciated.

df = pd.DataFrame({'time':['2021-12-31', '2022-03-31', '2022-06-30'],
                   'n' : [100, 200, 300]})
df.time =pd.to_datetime(df.time)

print(df.dtypes)

Output: 
time    datetime64[ns]
rate           float64
dtype: object
isinstance(df.time, datetime.datetime)
Output:
False

CodePudding user response:

IIUC select one value to scalar, in your solution pass Series, so correctly failed:

print(isinstance(df.time.iat[0], datetime.datetime))
True
  • Related