Skip to content Skip to sidebar Skip to footer

Cannot Convert From Pandas._libs.tslibs.timestamps.timestamp To Datetime.datetime

I´m trying to convert from pandas._libs.tslibs.timestamps.Timestamp to datetime.datetime but the change is not saved: type(df_unix.loc[45,'LastLoginDate']) OUTPUT: pandas._libs.t

Solution 1:

Suppose that you have this date:

some_date = pd.Timestamp("1970-01-01")
print(some_date)
Output: Timestamp('1970-01-01 00:00:00')

To transform it to datetime just do this:

some_date.to_pydatetime()

And you will get:

output: datetime.datetime(1970, 1, 1, 0, 0)

You can't use datetime.datetime(some_date) because the constructor of the datetime.datetime class takes 3 int numbers:

One for the year, one for the month and one for the day.

Solution 2:

df_unix.loc[45,'LastLoginDate'] returns a scalar value. pandas uses Timestamp for scalar values so the type is correct.

Run df_unix.info(). You should see the column type is still datetime64[ns].

You are also assigning one value at location 45 using to_pydatetime which is used for returning the native Python datetime object. It would be an interesting result if this changed the type for the whole column.

Post a Comment for "Cannot Convert From Pandas._libs.tslibs.timestamps.timestamp To Datetime.datetime"