Issues with datetime and isinstance()

Question:

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
Asked By: Henrik

||

Answers:

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

print(isinstance(df.time.iat[0], datetime.datetime))
True
Answered By: jezrael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.