Date Calculation (typeerror: Unsupported Operand Type(s) For -: 'str' And 'str')
I have a data set as below: date_time srch_co srch_ci 0 2014-11-03 16:02:28 2014-12-19 2014-12-15 1 2013-03-13 19:25:01 2013-03-14 2013-03-13 2 2014-10-13
Solution 1:
You need convert columns srch_co
and srch_ci
to_datetime
first and then use mask
for replace values less as 0
to NaN
(default value of mask
function):
sample["srch_co"]=pd.to_datetime(sample["srch_co"])sample["srch_ci"]=pd.to_datetime(sample["srch_ci"])sample["duration"]=(sample["srch_co"]-sample["srch_ci"])/np.timedelta64(1,'D')sample["days_in_advance"]=(sample["srch_co"]-sample["date_time"])/np.timedelta64(1,'D')cols= ['duration','days_in_advance']
sample[cols]=sample[cols].mask(sample[cols]<0)#first value of srch_ci column was changed for NaN outputprint(sample)date_timesrch_cosrch_cidurationdays_in_advance02014-11-03 16:02:28 2014-12-19 2015-12-15 NaN45.33162012013-03-13 19:25:01 2013-03-14 2013-03-13 1.00.19096122014-10-13 13:20:25 2015-04-10 2015-04-03 7.0178.44415532013-11-05 10:40:34 2013-11-08 2013-11-07 1.02.55516242014-06-10 13:34:56 2014-08-08 2014-08-03 5.058.43407452014-12-16 14:34:39 2014-12-17 2014-12-16 1.00.392604
Solution 2:
Seems like you're subtracting a string from a string. Make sure to convert the column to type 'date' using pd.to_datetime, and then you'll be able to subtract one day from another.
Another recommendation is to avoid for loops and to use vectorized operations, such as pd.DataFrame.subtract(series, axis=0), as that is one of the biggest advantages of using pandas over any simple list.
After you've calculated the difference, then you can turn all negatives into nan by saying
dataframe[dataframe['duration'] < 0] = np.nan
Post a Comment for "Date Calculation (typeerror: Unsupported Operand Type(s) For -: 'str' And 'str')"