Reputation: 3502
I have some time series data from an API request and when I am doing some data wrangling this error pops up below. The data wrangling is just some simple Pandas series math (not shown).
TypeError: unsupported operand type(s) for -: 'str' and 'str'
But when I save the data to a CSV:
elecMeter_df.to_csv('C:\\Python Scripts\\elecMeter_df.csv', sep=',', header=True, index=True, na_rep='N/A')
And then parse the dates on a read_CSV:
elecMeter_dfCSV = pd.read_csv('C:\\Python Scripts\\elecMeter_df.csv', index_col='Date', parse_dates=True)
I do not get the original error described above.. Why is that? Am I getting the error because the time stamp is a string and I need to convert into an integer format?
When I get the error, the index is in this format:
print(elecMeter_df.index)
But when read the CSV file and Parse the date column (No error in the data wrangling processes, the index is in this format: (no Chicago Time zone reference)
print(elecMeter_df.index)
Any help/tips that can be explained to me about time stamps and why this error happens would be greatly appreciated. Utilimetely I am trying to not have to use the read/write CSV process, but if its the only method to not get any errors Ill just stick with that!
Upvotes: 2
Views: 1440
Reputation: 2286
Not sure what code you are running to generate that error. However the time stamp probably needs to be converted from a string to a date time. Try using pd.to_datetime, additionally you can specify the format (list of options and meanings are provided below). The example I used for the format is year-month-day hour-minutes.
pd.to_datetime(df['column'], format = %Y-%m-%d %H:%M)
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%f Microsecond as a decimal number [0,999999], zero-padded on the left
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale’s equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week)
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week)
%x Locale’s appropriate date representation.
%X Locale’s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%z UTC offset in the form +HHMM or -HHMM.
%Z Time zone name (empty string if the object is naive).
%% A literal '%' character.
Upvotes: 1