0

I'm stuck on this and looking for community help. I have a few columns within a dataframe that are labeled as objects but would like to convert to datetime. All columns have year,month,day criteria.

time = np.array(['time1','time2','time3'])
def cols_to_datetime(df):

   cols_to_datetime = time

   for col in cols_to_datetime:
       df[col] = pd.to_datetime(df[col])

   return df

Although all have year, month, day i'm receiving this error

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

df -

time1                      time2                      time3 
2020-06-06 20:01:10.327    2020-06-06 22:08:14.832    2020-06-06

There may be nulls in the df that would need to be skipped.

I did test without them and didn't have luck. So I'm not quite sure why this approach doesn't work. Thanks!

3
  • Please post an example of your dataframe Commented Feb 13, 2021 at 20:55
  • beware using things like time for variable names... plus it will be data specific Commented Feb 13, 2021 at 20:58
  • i've added a simplified df sample Commented Feb 13, 2021 at 20:59

1 Answer 1

2

You can use DataFrame.astype().

import pandas as pd

def convert_times(df, cols=None):
    if not cols:
        # if no columns are specified, use all
        cols = df.columns
    df[cols] = df[cols].astype('datetime64')
    return df

df = pd.DataFrame({
    'created_at': ['2020-06-06 20:01:10.327'],
    'updated_at': ['2020-06-06 22:08:14.832']
})
print(df.dtypes)
# created_at    object
# updated_at    object
# dtype: object

df2 = convert_times(df)
print(df2.dtypes)
# created_at    datetime64[ns]
# updated_at    datetime64[ns]
# dtype: object
Sign up to request clarification or add additional context in comments.

4 Comments

if one of the dfs does not have all of the columns, how can i account for that easily?
Do you mean the case in which your dataframe has a lot of columns, but you only want to make a subset of those into datetime? Use convert_times(df, ['col1', 'col2']). All other columns of the dataframe won't be touched.
if a df has time1 only but another df has time1,time2,time3. i'm running this on a few dfs, so i get a not in index error if not all times columns are in the df
Then you would have to use convert_times(df1, ['time1', 'time2']); convert_times(df2, ['anothertime1', 'anothertime2'])

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.