自定义 Pandas 日历使其可迭代

huangapple go评论100阅读模式
英文:

Custom calendar in Pandas make iterable

问题

在下面的 NYSE 自定义日历中,使用 pandas 试图使该类可迭代,以运行以下检查:

  1. nyse_cal = NYSECalendar()
  2. for date in nyse_cal:
  3. print(date)

但语句 last_holiday = self.rules[-1] 由于索引 [-1] 而导致 TypeError: 'method' is not subscriptable,这是因为针对 'Holiday' 实例进行了索引。是否有解决方法?

  1. from pandas.tseries.offsets import CustomBusinessDay, MonthEnd
  2. from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, \
  3. USMemorialDay, USMartinLutherKingJr, USPresidentsDay, GoodFriday, \
  4. USLaborDay, USThanksgivingDay, nearest_workday, next_monday
  5. class NYSECalendar(AbstractHolidayCalendar):
  6. ''' 纽约证券交易所节假日日历 '''
  7. rules = [
  8. Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
  9. USMartinLutherKingJr,
  10. USPresidentsDay,
  11. GoodFriday,
  12. USMemorialDay,
  13. Holiday('Juneteenth', month=6, day=19, observance=nearest_workday),
  14. Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
  15. USLaborDay,
  16. USThanksgivingDay,
  17. Holiday('Christmas', month=12, day=25, observance=nearest_workday),
  18. ]
  19. def __iter__(self):
  20. last_holiday = self.rules[-1]
  21. last_holiday_date = last_holiday.dates[-1]
  22. start_date = last_holiday_date + MonthEnd(12) + CustomBusinessDay(calendar=self)
  23. end_date = start_date + MonthEnd(1) - CustomBusinessDay(calendar=self)
  24. curr_date = start_date
  25. while curr_date <= end_date:
  26. yield curr_date
  27. curr_date += CustomBusinessDay(calendar=self)
英文:

trying to make the class iterable for the below NYSE custom calendar using pandas, say to run this check:

  1. nyse_cal = NYSECalendar()
  2. for date in nyse_cal:
  3. print(date)

but the statement last_holiday = self.rules[-1] produces TypeError: &#39;method&#39; is not subscriptable for the 'Holiday' instance because of indexing [-1]. Is there a workaround?

  1. from pandas.tseries.offsets import CustomBusinessDay, MonthEnd
  2. from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, \
  3. USMemorialDay, USMartinLutherKingJr, USPresidentsDay, GoodFriday, \
  4. USLaborDay, USThanksgivingDay, nearest_workday, next_monday
  5. class NYSECalendar(AbstractHolidayCalendar):
  6. &#39;&#39;&#39; NYSE holiday calendar via pandas &#39;&#39;&#39;
  7. rules = [
  8. Holiday(&#39;New Years Day&#39;, month=1, day=1, observance=nearest_workday),
  9. USMartinLutherKingJr,
  10. USPresidentsDay,
  11. GoodFriday,
  12. USMemorialDay,
  13. Holiday(&#39;Juneteenth&#39;, month=6, day=19, observance=nearest_workday),
  14. Holiday(&#39;USIndependenceDay&#39;, month=7, day=4, observance=nearest_workday),
  15. USLaborDay,
  16. USThanksgivingDay,
  17. Holiday(&#39;Christmas&#39;, month=12, day=25, observance=nearest_workday),
  18. ]
  19. def __iter__(self):
  20. last_holiday = self.rules[-1]
  21. last_holiday_date = last_holiday.dates[-1]
  22. start_date = last_holiday_date + MonthEnd(12) + CustomBusinessDay(calendar=self)
  23. end_date = start_date + MonthEnd(1) - CustomBusinessDay(calendar=self)
  24. curr_date = start_date
  25. while curr_date &lt;= end_date:
  26. yield curr_date
  27. curr_date += CustomBusinessDay(calendar=self)

答案1

得分: 1

你可以这样做:

  1. from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, nearest_workday, USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay, USLaborDay, USThanksgivingDay
  2. from pandas.tseries.offsets import MonthEnd, CustomBusinessDay
  3. import pandas as pd
  4. class NYSECalendar(AbstractHolidayCalendar):
  5. rules = [
  6. Holiday('New Years Day', month=1, day=1, observance=nearest_workday),
  7. USMartinLutherKingJr,
  8. USPresidentsDay,
  9. GoodFriday,
  10. USMemorialDay,
  11. Holiday('Juneteenth', month=6, day=19, observance=nearest_workday),
  12. Holiday('USIndependenceDay', month=7, day=4, observance=nearest_workday),
  13. USLaborDay,
  14. USThanksgivingDay,
  15. Holiday('Christmas', month=12, day=25, observance=nearest_workday),
  16. ]
  17. def get_rules(self):
  18. return self.rules
  19. def __iter__(self):
  20. last_holiday = self.get_rules()[-1]
  21. last_holiday_date = last_holiday.dates[-1]
  22. start_date = last_holiday_date + MonthEnd(12) + CustomBusinessDay(calendar=self)
  23. end_date = start_date + MonthEnd(1) - CustomBusinessDay(calendar=self)
  24. curr_date = start_date
  25. while curr_date <= end_date:
  26. yield curr_date
  27. curr_date += CustomBusinessDay(calendar=self)

并在这个样本数据框中测试它,如下:

  1. import pandas as pd
  2. nyse_cal = NYSECalendar()
  3. start_date = '2022-01-01'
  4. end_date = '2022-01-31'
  5. df = pd.DataFrame({
  6. 'business_day': pd.date_range(start=start_date, end=end_date, freq=CustomBusinessDay(calendar=nyse_cal))
  7. })
  8. print(df)

返回工作日:

  1. business_day
  2. 0 2022-01-03
  3. 1 2022-01-04
  4. 2 2022-01-05
  5. 3 2022-01-06
  6. 4 2022-01-07
  7. 5 2022-01-10
  8. 6 2022-01-11
  9. 7 2022-01-12
  10. 8 2022-01-13
  11. 9 2022-01-14
  12. 10 2022-01-18
  13. 11 2022-01-19
  14. 12 2022-01-20
  15. 13 2022-01-21
  16. 14 2022-01-24
  17. 15 2022-01-25
  18. 16 2022-01-26
  19. 17 2022-01-27
  20. 18 2022-01-28
  21. 19 2022-01-31

基本上,你需要获取正确的包。

英文:

You cn do this:

  1. from pandas.tseries.holiday import AbstractHolidayCalendar, Holiday, nearest_workday,USMartinLutherKingJr, USPresidentsDay, GoodFriday, USMemorialDay, USLaborDay,USThanksgivingDay
  2. from pandas.tseries.offsets import MonthEnd, CustomBusinessDay
  3. import pandas as pd
  4. class NYSECalendar(AbstractHolidayCalendar):
  5. rules = [
  6. Holiday(&#39;New Years Day&#39;, month=1, day=1, observance=nearest_workday),
  7. USMartinLutherKingJr,
  8. USPresidentsDay,
  9. GoodFriday,
  10. USMemorialDay,
  11. Holiday(&#39;Juneteenth&#39;, month=6, day=19, observance=nearest_workday),
  12. Holiday(&#39;USIndependenceDay&#39;, month=7, day=4, observance=nearest_workday),
  13. USLaborDay,
  14. USThanksgivingDay,
  15. Holiday(&#39;Christmas&#39;, month=12, day=25, observance=nearest_workday),
  16. ]
  17. def get_rules(self):
  18. return self.rules
  19. def __iter__(self):
  20. last_holiday = self.get_rules()[-1]
  21. last_holiday_date = last_holiday.dates[-1]
  22. start_date = last_holiday_date + MonthEnd(12) + CustomBusinessDay(calendar=self)
  23. end_date = start_date + MonthEnd(1) - CustomBusinessDay(calendar=self)
  24. curr_date = start_date
  25. while curr_date &lt;= end_date:
  26. yield curr_date
  27. curr_date += CustomBusinessDay(calendar=self)

and test it it this sample dataframe, this way:

  1. import pandas as pd
  2. nyse_cal = NYSECalendar()
  3. start_date = &#39;2022-01-01&#39;
  4. end_date = &#39;2022-01-31&#39;
  5. df = pd.DataFrame({
  6. &#39;business_day&#39;: pd.date_range(start=start_date, end=end_date, freq=CustomBusinessDay(calendar=nyse_cal))
  7. })
  8. print(df)

return th business days:

  1. business_day
  2. 0 2022-01-03
  3. 1 2022-01-04
  4. 2 2022-01-05
  5. 3 2022-01-06
  6. 4 2022-01-07
  7. 5 2022-01-10
  8. 6 2022-01-11
  9. 7 2022-01-12
  10. 8 2022-01-13
  11. 9 2022-01-14
  12. 10 2022-01-18
  13. 11 2022-01-19
  14. 12 2022-01-20
  15. 13 2022-01-21
  16. 14 2022-01-24
  17. 15 2022-01-25
  18. 16 2022-01-26
  19. 17 2022-01-27
  20. 18 2022-01-28
  21. 19 2022-01-31

Basically, you need to get th right packages.

huangapple
  • 本文由 发表于 2023年2月23日 22:13:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定