-1

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.tslibs.timestamps.Timestamp

type(df_unix.loc[45,'LastLoginDate'].to_pydatetime())

OUTPUT: datetime.datetime

df_unix.loc[45,'LastLoginDate'] = df_unix.loc[i,'LastLoginDate'].to_pydatetime()

type(df_unix.loc[45,'LastLoginDate'])

OUTPUT: pandas._libs.tslibs.timestamps.Timestamp

2 Answers2

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.

ljuk
  • 701
  • 3
  • 12
0

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.

Anthony
  • 363
  • 3
  • 9