1

Hi I have following data stored in csv

Item Name 2020-12-08 00:00:00  2020-13-08 00:00:00   2020-14-08 00:00:00
Item1          43                   33                     11
Item2          44                   33                     24
Item3          32                   78                     89

So when I import csv the column index is stored as str. But I want it in datetime object.

When I used pd.to_datetime(temppppp.columns), it is returning me Timestamp. I need datetime object here.

Any way to solve this? Or should I store in excel which preserves data-type but takes lot more disk-space and time to execute

  • Using `pd.to_datetime` on either index should result in a `DatetimeIndex`. The `datetime64[ns]` dtype is a container that holds `pandas._libs.tslibs.timestamps.Timestamp`values. There is no `datetime64[ns]` type for an individual value – ALollz Nov 18 '20 at 16:38
  • Yes. I was looking for data type of datetime library – Anmol Sureka Nov 18 '20 at 16:40
  • When I do this ```df.iloc[:, 1:4] = df.iloc[:, 1:4].apply(pd.to_datetime, errors='coerce')``` all the columns I choose are ```datetime64[ns]```. Isn't that what you're looking for? Or have I missunderstood what you wish to do? – Serge de Gosson de Varennes Nov 18 '20 at 17:03
  • 1
    do you want the ***column names*** to be datetime objects? – FObersteiner Nov 18 '20 at 17:18
  • Yes, @MrFuppes . datetime objects – Anmol Sureka Nov 18 '20 at 17:27
  • I have tried numerous methods. For ex. I took out column name into a list. converted to a datetime object using datetime.datetime.strptime(). and then finally renamed those columns but they are still coming out to be timestamp object. Really frustrating – Anmol Sureka Nov 18 '20 at 17:30

0 Answers0