-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
  • please put a sample input of your dates, or at least a copy of your traceback Commented Jan 4, 2021 at 13:32
  • Timestamp('1970-01-01 00:00:00') Commented Jan 4, 2021 at 13:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.