I have a data set and needs to calculate daily average and weekly average. I know this can be done by Pandas. Below is the data and code that I have till now;
date T1 T2 T3
12/17/13 00:28:38 19 23.1 7.3
12/17/13 00:58:38 19 22.9 7.3
12/17/13 01:28:38 18.9 22.8 6.3
12/17/13 01:58:38 18.9 23.1 6.3
12/17/13 02:28:38 18.8 23 6.3
12/17/13 02:58:38 18.8 22.9 6.3
.......
12/18/13 00:28:07 19.5 22.4 5.3
12/18/13 00:58:08 19.4 22.3 5.3
12/18/13 01:28:07 19.4 22.1 5.3
.......
3/22/14 16:55:18 17.7 20.6 10.1
3/22/14 17:08:31 17.7 20.6 10.1
3/22/14 17:26:04 17.6 20.5 8
3/22/14 17:56:04 17.7 20.5 7
and the code that I have till now is;
import pandas as pd
Temp=pd.read_csv("Book1.csv",parse_dates=['date'])
Temp=Temp.set_index('date')
In [25]: Temp_plot.head()
Temp_plot=Temp.resample('W',how='mean')
Temp_plot.head()
Out[25]:
T1 T2 T3
date
2013-12-22 18.740345 35.055517 7.532414
2013-12-29 14.501770 14.950442 6.497935
2014-01-05 13.135207 14.064793 7.795858
2014-01-12 17.296154 38.503550 7.827219
2014-01-19 18.217699 38.892625 6.952212
The problem is now I have to delete some mean weekly values that were holidays and should not be included in the resulted mean values. Shall I have one list that contains the dates that should not be included and then comparing values in the Temp_plot?
I have added a list Wase = ["2013-12-22","2014-01-05"]
, as suggested in comments and used Temp_plot1 = Temp_plot.drop(Wase)
Now I got any error, which says ValueError: labels ['2013-12-22' '2014-01-05'] not contained in axis
. Any idea how to remove this error as I have to delete row that contains dates contain in list.
You need to create a calendar of holidays using dt.date(year, month, day)
. Then you filter the holidays from the index using a list comprehension structure as shown below. Lastly, you select these filtered dates using .ix
which selects data from a dataframe based on the index value.
import datetime as dt
holidays = [dt.date(2015, 12, 25), ...]
idx = [timestamp for timestamp in Temp.index if timestamp.date() not in holidays]
Temp_plot = Temp.ix[idx].resample('W', how='mean')
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments