Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issues with datetime and isinstance()

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

print(isinstance(df.time.iat[0], datetime.datetime))
True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading