test_pct_change.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. Series,
  5. date_range,
  6. )
  7. import pandas._testing as tm
  8. class TestSeriesPctChange:
  9. def test_pct_change(self, datetime_series):
  10. rs = datetime_series.pct_change(fill_method=None)
  11. tm.assert_series_equal(rs, datetime_series / datetime_series.shift(1) - 1)
  12. rs = datetime_series.pct_change(2)
  13. filled = datetime_series.fillna(method="pad")
  14. tm.assert_series_equal(rs, filled / filled.shift(2) - 1)
  15. rs = datetime_series.pct_change(fill_method="bfill", limit=1)
  16. filled = datetime_series.fillna(method="bfill", limit=1)
  17. tm.assert_series_equal(rs, filled / filled.shift(1) - 1)
  18. rs = datetime_series.pct_change(freq="5D")
  19. filled = datetime_series.fillna(method="pad")
  20. tm.assert_series_equal(
  21. rs, (filled / filled.shift(freq="5D") - 1).reindex_like(filled)
  22. )
  23. def test_pct_change_with_duplicate_axis(self):
  24. # GH#28664
  25. common_idx = date_range("2019-11-14", periods=5, freq="D")
  26. result = Series(range(5), common_idx).pct_change(freq="B")
  27. # the reason that the expected should be like this is documented at PR 28681
  28. expected = Series([np.NaN, np.inf, np.NaN, np.NaN, 3.0], common_idx)
  29. tm.assert_series_equal(result, expected)
  30. def test_pct_change_shift_over_nas(self):
  31. s = Series([1.0, 1.5, np.nan, 2.5, 3.0])
  32. chg = s.pct_change()
  33. expected = Series([np.nan, 0.5, 0.0, 2.5 / 1.5 - 1, 0.2])
  34. tm.assert_series_equal(chg, expected)
  35. @pytest.mark.parametrize(
  36. "freq, periods, fill_method, limit",
  37. [
  38. ("5B", 5, None, None),
  39. ("3B", 3, None, None),
  40. ("3B", 3, "bfill", None),
  41. ("7B", 7, "pad", 1),
  42. ("7B", 7, "bfill", 3),
  43. ("14B", 14, None, None),
  44. ],
  45. )
  46. def test_pct_change_periods_freq(
  47. self, freq, periods, fill_method, limit, datetime_series
  48. ):
  49. # GH#7292
  50. rs_freq = datetime_series.pct_change(
  51. freq=freq, fill_method=fill_method, limit=limit
  52. )
  53. rs_periods = datetime_series.pct_change(
  54. periods, fill_method=fill_method, limit=limit
  55. )
  56. tm.assert_series_equal(rs_freq, rs_periods)
  57. empty_ts = Series(index=datetime_series.index, dtype=object)
  58. rs_freq = empty_ts.pct_change(freq=freq, fill_method=fill_method, limit=limit)
  59. rs_periods = empty_ts.pct_change(periods, fill_method=fill_method, limit=limit)
  60. tm.assert_series_equal(rs_freq, rs_periods)
  61. @pytest.mark.parametrize("fill_method", ["pad", "ffill", None])
  62. def test_pct_change_with_duplicated_indices(fill_method):
  63. # GH30463
  64. s = Series([np.nan, 1, 2, 3, 9, 18], index=["a", "b"] * 3)
  65. result = s.pct_change(fill_method=fill_method)
  66. expected = Series([np.nan, np.nan, 1.0, 0.5, 2.0, 1.0], index=["a", "b"] * 3)
  67. tm.assert_series_equal(result, expected)