0

I have a data frame with a column with number of dates since year 1900. I need to change the format to Y%m%d. For this I created a column datetime.date(1900,1,1) to which try to sum the number of days.

I am receiving this error:

TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'TimedeltaIndex'

This is my code:

df_vend['creation_date'] = np.where(df_vend['creation_date'] == 0,1,df_vend['creation_date'])
df_vend['base_date'] = [datetime.date(2019,1,1)] * len(df_vend.creation_date)
df_vend['creation_date'] = df_vend['base_date'] + pd.to_timedelta(df_vend['creation_date'])

np.where is used to change dates with value 0.

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • 1
    Try replacing the second line with `df_vend['base_date'] = pd.to_datetime(datetime.date(2019,1,1))` – Michael Gardner Oct 02 '19 at 23:59
  • That worked. I had a couple of errors more, the final code is (of course the base date is 1900,1,1 not 2019,1,1: `df_vend['creation_date'] = np.where(df_vend['creation_date'] == 0,1,df_vend['creation_date']) df_vend['base_date'] = [pd.to_datetime(datetime.date(1900,1,1))] * len(df_vend.creation_date) df_vend['creation_date'] = df_vend['base_date'] + pd.to_timedelta(df_vend['creation_date'],unit='days')` Why is it necessary to use datetime.date, when I already used to_datetime? – Josue Barrantes Oct 03 '19 at 12:24
  • 1
    You don't have to but you also do not need to multiply it by the length of the array as pandas will broadcast it for you. You can remove line 2 completely. `df_vend['creation_date'] = pd.to_datetime('1/1/1900') + pd.to_timedelta(df_vend['creation_date'])` – Michael Gardner Oct 03 '19 at 14:07

0 Answers0