2

I get the following error when trying to convert multiple the unix timestamps to a datetime in pandas, I've tried setting values but seem to not be getting it right.

ValueError: to assemble mappings requires at least that [year, month, day] be specified: [day,month,year] is missing

Here's the code:

pd.to_datetime(ksdata[['state_changed_at','created_at','launched_at']])

It's got to be something simple but I just can't see it.

Sample of file

enter image description here

2
  • Can you post ksdata.head()? Commented Oct 31, 2017 at 16:35
  • @Vaishali added a screenshot of the header couldn't find a more elegant way to do it :) Commented Oct 31, 2017 at 16:41

1 Answer 1

4

The dataset helps as its easier to test the solution but anyway for unix timestamp, solution would be

ksdata[['state_changed_at','created_at','launched_at']] = ksdata[['state_changed_at','created_at','launched_at']].\
apply(pd.to_datetime, unit = 's')

As @MaxU suggested, if you need to convert all the columns ending with '_at' to timestamp, you can filter them using

ksdata.filter(regex='_at$') 
Sign up to request clarification or add additional context in comments.

4 Comments

we can try to filter columns ending with _at like this: ksdata.filter(regex='_at$'), if OP doesn't have other columns (not UNIX timestamps) ending with _at...
@MaxU, thanks! Let me will update the answer. Only thing is, we don't know how many columns are present in the dataframe and how do they look like.
@Vaishali thank you! I always forget about the apply function! I must try remember this :)
@MaxU thanks for highlighting that I can use filter, again something that I keep forgetting exists.

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.