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

1
  • What is print (df.head(5)).to_dict() ? Because hard test with sample data, I cannot convert second row to datetimes. Commented Nov 2, 2020 at 5:45

1 Answer 1

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.

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

2 Comments

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.
I mocked up my own data and tested it out. datetime needs to be accessed by datetime.datetime which is most likely your issue.

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.