0

I have a column with datetime and integer values..However i want to convert even the integer values to datetikme as well.

StartTime             EndTime               Source 
2170-01-01 00:00:00   2170-01-01 00:00:00   NA
1.60405e+18           1.60405e+18           Arm_dearm

I tried using

pd.to_datetime(site_list['StartTime'])

but it yields the same result.

And Ultimately i am not able to run the following line.

np.where(site_list["StartTime"].dt.date!=site_list["EndTime"].dt.date,"armed next day",site_list["Source"])

which throws the following error:

mixed datetimes and integers in passed array

Harish reddy
  • 431
  • 2
  • 9
  • What is `print (df.head(5)).to_dict()` ? Because hard test with sample data, I cannot convert second row to datetimes. – jezrael Nov 02 '20 at 05:45

1 Answers1

1

The issue as the error states is that there are mixed types in the columns so it will not convert it. I'm going to assume it is a UNIX timestamp. Check this answer and see if maybe this is the time format you are dealing with.

We can get around this by doing something like this:

import datetime
def convert(time):
    return datetime.datetime.fromtimestamp(time)

site_list["StartTime"] = site_list["StartTime"].apply(lambda x: convert(x) if isinstance(x, int) else x)

This will check each value in the StartTime column, then check if it is a integer or not. If it is then it will convert it to the datetime type. If is not a integer it will leave it alone.

  • Does it give a error? Also can you provide a snippet of the data? It is hard to judge what needs to be done without more information. jerzael made a comment on your post, you can just provide that. – StealthBadger747 Nov 02 '20 at 06:08
  • 1
    I mocked up my own data and tested it out. datetime needs to be accessed by datetime.datetime which is most likely your issue. – StealthBadger747 Nov 02 '20 at 06:20