0

I have a pandas dataframe with a "datetime" column for which when I run df.info(), Dtype is shown as Object. However, if I check each value, they are all <class datetime.datetime> why is it inconsistent and how can I make it datetime or idealy <class pandas._libs.tslibs.timestamps.Timestamp>?

Jack Smith
  • 71
  • 6
  • 2
    Without an [mre] it might be hard to help. ... [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Jul 14 '21 at 14:23
  • 1
    Might be some row of your column contain "none " or something else, by the way could you plz show your dataframe if possible. – sam2611 Jul 14 '21 at 14:24
  • Do your dates contain multiple different time zones / UTC offsets? – FObersteiner Jul 14 '21 at 17:14
  • I cant really provide the data Im working with. I did think of None and those are removed completely. I even ran df["date_dtype"] = df.date.apply(lambda x: type(x)) and they seem to be all datetime.datetime. – Jack Smith Jul 14 '21 at 21:59
  • there are, however, some dirty data where year = 4024, etc. not sure if it corrupts the data? – Jack Smith Jul 17 '21 at 19:04

1 Answers1

0

Without providing data in your example, you could try this code to check all cells if they have the same type:

 df['column_name'].apply(type).eq(datetime.datetime).all()

If the previous line returns False that means you have some cells not having datetime type.

To convert this column to <class pandas._libs.tslibs.timestamps.Timestamp>, you could use this:

from pandas._libs.tslibs.timestamps import Timestamp
df['column_name'] = Timestamp(df['column_name'])