Reputation: 21
I have an MS Excel table with Date in column A and time in Column B, C, D, E, F.
After loading the table in a data frame, I want to subtract 1 hour from column B, C, D, E, F.
Here is the code:
df = pd.read_excel(filename)
df.columns=['Date','ColB','ColC','ColD','ColE', 'ColF']
df['Delta'] = df['ColB'].apply(lambda x: timedelta(hours=1))#Add column for 1 hour
df['ColB_New']=df['ColB'] + df['Delta']
The error I get is:
'TypeError: ufunc add cannot use operands with types dtype('O') and dtype('
Please suggest a solution.
Here is the sample data:
Upvotes: 0
Views: 281
Reputation: 136
That error is letting you know that one or more of your columns contains non-datetime rows. Check the columns with something like df.dtypes
. I often find when reading from excel that I have to convert my columns with pd.to_datetime
, say df['ColB'] = pd.to_datetime(df['ColB'])
.
Upvotes: 1