我有一个日期的DataFrame,想要过滤特定的日期+ - 几天。
import pandas as pd
import numpy as np
import datetime
dates = pd.date_range(start="08/01/2009",end="08/01/2012",freq="D")
df = pd.DataFrame(np.random.rand(len(dates), 1)*1500, index=dates, columns=['Power'])
如果我选择让我们说日期2009-08-03
和一个5
天的窗口,输出将类似于:
>>>
Power
2010-07-29 713.108020
2010-07-30 1055.109543
2010-07-31 951.159099
2010-08-01 1350.638983
2010-08-02 453.166697
2010-08-03 1066.859386
2010-08-04 1381.900717
2010-08-05 107.489179
2010-08-06 1195.945723
2010-08-07 1209.762910
2010-08-08 349.554492
N.B。:我想要完成的原始问题是Python: Filter DataFrame in Pandas by hour, day and month grouped by year
答案 0 :(得分:1)
我为实现此目的而创建的函数是filterDaysWindow
,可以按如下方式使用:
import pandas as pd
import numpy as np
import datetime
dates = pd.date_range(start="08/01/2009",end="08/01/2012",freq="D")
df = pd.DataFrame(np.random.rand(len(dates), 1)*1500, index=dates, columns=['Power'])
def filterDaysWindow(df, date, daysWindow):
"""
Filter a Dataframe by a date within a window of days
@type df: DataFrame
@param df: DataFrame of dates
@type date: datetime.date
@param date: date to focus on
@type daysWindow: int
@param daysWindow: Number of days to perform the days window selection
@rtype: DataFrame
@return: Returns a DataFrame with dates within date+-daysWindow
"""
dateStart = date - datetime.timedelta(days=daysWindow)
dateEnd = date + datetime.timedelta(days=daysWindow)
return df [dateStart:dateEnd]
df_filtered = filterDaysWindow(df, datetime.date(2010,8,3), 5)
print df_filtered