英文:
Custom calendar in Pandas make iterable
问题
在下面的 NYSE 自定义日历中,使用 pandas
试图使该类可迭代,以运行以下检查:
nyse_cal = NYSECalendar()
for date in nyse_cal:
print(date)
但语句 last_holiday = self.rules[-1]
由于索引 [-1]
而导致 TypeError: 'method' is not subscriptable
,这是因为针对 'Holiday' 实例进行了索引。是否有解决方法?
from pandas.tseries.offsets import CustomBusinessDay, MonthEnd
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, \
USMemorialDay, USMartinLutherKingJr, USPresidentsDay, GoodFriday, \
USLaborDay, USThanksgivingDay, nearest_workday, next_monday
class NYSECalendar(AbstractHolidayCalendar):
''' 纽约证券交易所节假日日历 '''
rules = [
Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
USMartinLutherKingJr,
USPresidentsDay,
GoodFriday,
USMemorialDay,
Holiday('Juneteenth', month=6, day=19, observance=nearest_workday),
Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
USLaborDay,
USThanksgivingDay,
Holiday('Christmas', month=12, day=25, observance=nearest_workday),
]
def __iter__(self):
last_holiday = self.rules[-1]
last_holiday_date = last_holiday.dates[-1]
start_date = last_holiday_date + MonthEnd(12) + CustomBusinessDay(calendar=self)
end_date = start_date + MonthEnd(1) - CustomBusinessDay(calendar=self)
curr_date = start_date
while curr_date <= end_date:
yield curr_date
curr_date += CustomBusinessDay(calendar=self)
英文:
trying to make the class iterable for the below NYSE custom calendar using pandas
, say to run this check:
nyse_cal = NYSECalendar()
for date in nyse_cal:
print(date)
but the statement last_holiday = self.rules[-1]
produces TypeError: 'method' is not subscriptable
for the 'Holiday' instance because of indexing [-1]
. Is there a workaround?
from pandas.tseries.offsets import CustomBusinessDay, MonthEnd
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, \
USMemorialDay, USMartinLutherKingJr, USPresidentsDay, GoodFriday, \
USLaborDay, USThanksgivingDay, nearest_workday, next_monday
class NYSECalendar(AbstractHolidayCalendar):
''' NYSE holiday calendar via pandas '''
rules = [
Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
USMartinLutherKingJr,
USPresidentsDay,
GoodFriday,
USMemorialDay,
Holiday('Juneteenth', month=6, day=19, observance=nearest_workday),
Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
USLaborDay,
USThanksgivingDay,
Holiday('Christmas', month=12, day=25, observance=nearest_workday),
]
def __iter__(self):
last_holiday = self.rules[-1]
last_holiday_date = last_holiday.dates[-1]
start_date = last_holiday_date + MonthEnd(12) + CustomBusinessDay(calendar=self)
end_date = start_date + MonthEnd(1) - CustomBusinessDay(calendar=self)
curr_date = start_date
while curr_date <= end_date:
yield curr_date
curr_date += CustomBusinessDay(calendar=self)
答案1
得分: 1
你可以这样做:
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, nearest_workday, USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay, USLaborDay, USThanksgivingDay
from pandas.tseries.offsets import MonthEnd, CustomBusinessDay
import pandas as pd
class NYSECalendar(AbstractHolidayCalendar):
rules = [
Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
USMartinLutherKingJr,
USPresidentsDay,
GoodFriday,
USMemorialDay,
Holiday('Juneteenth', month=6, day=19, observance=nearest_workday),
Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
USLaborDay,
USThanksgivingDay,
Holiday('Christmas', month=12, day=25, observance=nearest_workday),
]
def get_rules(self):
return self.rules
def __iter__(self):
last_holiday = self.get_rules()[-1]
last_holiday_date = last_holiday.dates[-1]
start_date = last_holiday_date + MonthEnd(12) + CustomBusinessDay(calendar=self)
end_date = start_date + MonthEnd(1) - CustomBusinessDay(calendar=self)
curr_date = start_date
while curr_date <= end_date:
yield curr_date
curr_date += CustomBusinessDay(calendar=self)
并在这个样本数据框中测试它,如下:
import pandas as pd
nyse_cal = NYSECalendar()
start_date = '2022-01-01'
end_date = '2022-01-31'
df = pd.DataFrame({
'business_day': pd.date_range(start=start_date, end=end_date, freq=CustomBusinessDay(calendar=nyse_cal))
})
print(df)
返回工作日:
business_day
0 2022-01-03
1 2022-01-04
2 2022-01-05
3 2022-01-06
4 2022-01-07
5 2022-01-10
6 2022-01-11
7 2022-01-12
8 2022-01-13
9 2022-01-14
10 2022-01-18
11 2022-01-19
12 2022-01-20
13 2022-01-21
14 2022-01-24
15 2022-01-25
16 2022-01-26
17 2022-01-27
18 2022-01-28
19 2022-01-31
基本上,你需要获取正确的包。
英文:
You cn do this:
from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, nearest_workday,USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay, USLaborDay,USThanksgivingDay
from pandas.tseries.offsets import MonthEnd, CustomBusinessDay
import pandas as pd
class NYSECalendar(AbstractHolidayCalendar):
rules = [
Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
USMartinLutherKingJr,
USPresidentsDay,
GoodFriday,
USMemorialDay,
Holiday('Juneteenth', month=6, day=19, observance=nearest_workday),
Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
USLaborDay,
USThanksgivingDay,
Holiday('Christmas', month=12, day=25, observance=nearest_workday),
]
def get_rules(self):
return self.rules
def __iter__(self):
last_holiday = self.get_rules()[-1]
last_holiday_date = last_holiday.dates[-1]
start_date = last_holiday_date + MonthEnd(12) + CustomBusinessDay(calendar=self)
end_date = start_date + MonthEnd(1) - CustomBusinessDay(calendar=self)
curr_date = start_date
while curr_date <= end_date:
yield curr_date
curr_date += CustomBusinessDay(calendar=self)
and test it it this sample dataframe, this way:
import pandas as pd
nyse_cal = NYSECalendar()
start_date = '2022-01-01'
end_date = '2022-01-31'
df = pd.DataFrame({
'business_day': pd.date_range(start=start_date, end=end_date, freq=CustomBusinessDay(calendar=nyse_cal))
})
print(df)
return th business days:
business_day
0 2022-01-03
1 2022-01-04
2 2022-01-05
3 2022-01-06
4 2022-01-07
5 2022-01-10
6 2022-01-11
7 2022-01-12
8 2022-01-13
9 2022-01-14
10 2022-01-18
11 2022-01-19
12 2022-01-20
13 2022-01-21
14 2022-01-24
15 2022-01-25
16 2022-01-26
17 2022-01-27
18 2022-01-28
19 2022-01-31
Basically, you need to get th right packages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论