0

I have a data frame which looks like this:

                     A
0  1900-01-01 08:00:00
1  1900-01-01 14:30:00
2  1900-01-01 17:00:00
3  1900-01-01 14:00:00
4  1900-01-01 19:00:00

Each of the values is a Timestamp:

<<<df.loc[0,"A"]
Timestamp('1900-01-01 08:00:00')

I'd like to create a new column where the values are datetime rather than Timestamp. I thought this was the way to do it:

df["A2"] = pd.to_datetime(df["A"])

But the new column is also a series of Timestamps:

                     A                  A2
0  1900-01-01 08:00:00 1900-01-01 08:00:00
1  1900-01-01 14:30:00 1900-01-01 14:30:00
2  1900-01-01 17:00:00 1900-01-01 17:00:00
3  1900-01-01 14:00:00 1900-01-01 14:00:00
4  1900-01-01 19:00:00 1900-01-01 19:00:00

<<<df.loc[0,"A2"]
Timestamp('1900-01-01 08:00:00')

What am I doing wrong?

OD1995
  • 1,647
  • 4
  • 22
  • 52
  • I don't understand the problem. What's wrong with `pd.Timestamp`? Despite its name, it also holds date info.. `"pd.Timestamp is a wrapper around a numpy.datetime64. It is backed by the same int64 value, but supports the entire datetime.datetime interface, along with useful pandas-specific functionality."` [**source**](https://stackoverflow.com/a/49758140/9209546) – jpp Jul 18 '18 at 17:19
  • The column is actually a `datetime64[ns]` column. You can see this with `df.dtypes` – ALollz Jul 18 '18 at 17:25
  • 1
    The referenced q&a does not answer the OP's question. It works for individual Timestamps, but when converting a column, Pandas converts them right back. Consider: df = pd.DataFrame({"now": [datetime.datetime.utcnow()]}) ; df["now2"] = pd.to_datetime(df["now"]) ; df.dtypes – smontanaro Jul 19 '18 at 19:42
  • Solution is to explicitly create a Series with the conversion, setting dtype=object, then assign that to the "A2" column of your data frame – smontanaro Jul 19 '18 at 19:49

0 Answers0