make_holidays.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the BSD-style license found in the
  5. # LICENSE file in the root directory of this source tree. An additional grant
  6. # of patent rights can be found in the PATENTS file in the same directory.
  7. from __future__ import absolute_import
  8. from __future__ import division
  9. from __future__ import print_function
  10. from __future__ import unicode_literals
  11. import pandas as pd
  12. import numpy as np
  13. import warnings
  14. import holidays as hdays_part1
  15. import fbprophet.hdays as hdays_part2
  16. def get_holiday_names(country):
  17. """Return all possible holiday names of given country
  18. Parameters
  19. ----------
  20. country: country name
  21. Returns
  22. ------- a
  23. Dataframe with 'ds' and 'holiday', which can directly feed
  24. to 'holidays' params in Prophet
  25. """
  26. years = np.arange(1995, 2045)
  27. try:
  28. with warnings.catch_warnings():
  29. warnings.simplefilter("ignore")
  30. holiday_names = getattr(hdays_part2, country)(years=years).values()
  31. except AttributeError:
  32. try:
  33. holiday_names = getattr(hdays_part1, country)(years=years).values()
  34. except AttributeError:
  35. raise AttributeError(
  36. "Holidays in {} are not currently supported!".format(country))
  37. return set(holiday_names)
  38. def make_holidays_df(year_list, country):
  39. """Make dataframe of holidays for given years and countries
  40. Parameters
  41. ----------
  42. year_list: a list of years
  43. country: country name
  44. Returns
  45. -------
  46. Dataframe with 'ds' and 'holiday', which can directly feed
  47. to 'holidays' params in Prophet
  48. """
  49. try:
  50. holidays = getattr(hdays_part2, country)(years=year_list)
  51. except AttributeError:
  52. try:
  53. holidays = getattr(hdays_part1, country)(years=year_list)
  54. except AttributeError:
  55. raise AttributeError(
  56. "Holidays in {} are not currently supported!".format(country))
  57. holidays_df = pd.DataFrame(list(holidays.items()), columns=['ds', 'holiday'])
  58. holidays_df.reset_index(inplace=True, drop=True)
  59. holidays_df['ds'] = pd.to_datetime(holidays_df['ds'])
  60. return (holidays_df)