test_pct_change.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. DataFrame,
  5. Series,
  6. )
  7. import pandas._testing as tm
  8. class TestDataFramePctChange:
  9. @pytest.mark.parametrize(
  10. "periods,fill_method,limit,exp",
  11. [
  12. (1, "ffill", None, [np.nan, np.nan, np.nan, 1, 1, 1.5, 0, 0]),
  13. (1, "ffill", 1, [np.nan, np.nan, np.nan, 1, 1, 1.5, 0, np.nan]),
  14. (1, "bfill", None, [np.nan, 0, 0, 1, 1, 1.5, np.nan, np.nan]),
  15. (1, "bfill", 1, [np.nan, np.nan, 0, 1, 1, 1.5, np.nan, np.nan]),
  16. (-1, "ffill", None, [np.nan, np.nan, -0.5, -0.5, -0.6, 0, 0, np.nan]),
  17. (-1, "ffill", 1, [np.nan, np.nan, -0.5, -0.5, -0.6, 0, np.nan, np.nan]),
  18. (-1, "bfill", None, [0, 0, -0.5, -0.5, -0.6, np.nan, np.nan, np.nan]),
  19. (-1, "bfill", 1, [np.nan, 0, -0.5, -0.5, -0.6, np.nan, np.nan, np.nan]),
  20. ],
  21. )
  22. def test_pct_change_with_nas(
  23. self, periods, fill_method, limit, exp, frame_or_series
  24. ):
  25. vals = [np.nan, np.nan, 1, 2, 4, 10, np.nan, np.nan]
  26. obj = frame_or_series(vals)
  27. res = obj.pct_change(periods=periods, fill_method=fill_method, limit=limit)
  28. tm.assert_equal(res, frame_or_series(exp))
  29. def test_pct_change_numeric(self):
  30. # GH#11150
  31. pnl = DataFrame(
  32. [np.arange(0, 40, 10), np.arange(0, 40, 10), np.arange(0, 40, 10)]
  33. ).astype(np.float64)
  34. pnl.iat[1, 0] = np.nan
  35. pnl.iat[1, 1] = np.nan
  36. pnl.iat[2, 3] = 60
  37. for axis in range(2):
  38. expected = pnl.ffill(axis=axis) / pnl.ffill(axis=axis).shift(axis=axis) - 1
  39. result = pnl.pct_change(axis=axis, fill_method="pad")
  40. tm.assert_frame_equal(result, expected)
  41. def test_pct_change(self, datetime_frame):
  42. rs = datetime_frame.pct_change(fill_method=None)
  43. tm.assert_frame_equal(rs, datetime_frame / datetime_frame.shift(1) - 1)
  44. rs = datetime_frame.pct_change(2)
  45. filled = datetime_frame.fillna(method="pad")
  46. tm.assert_frame_equal(rs, filled / filled.shift(2) - 1)
  47. rs = datetime_frame.pct_change(fill_method="bfill", limit=1)
  48. filled = datetime_frame.fillna(method="bfill", limit=1)
  49. tm.assert_frame_equal(rs, filled / filled.shift(1) - 1)
  50. rs = datetime_frame.pct_change(freq="5D")
  51. filled = datetime_frame.fillna(method="pad")
  52. tm.assert_frame_equal(
  53. rs, (filled / filled.shift(freq="5D") - 1).reindex_like(filled)
  54. )
  55. def test_pct_change_shift_over_nas(self):
  56. s = Series([1.0, 1.5, np.nan, 2.5, 3.0])
  57. df = DataFrame({"a": s, "b": s})
  58. chg = df.pct_change()
  59. expected = Series([np.nan, 0.5, 0.0, 2.5 / 1.5 - 1, 0.2])
  60. edf = DataFrame({"a": expected, "b": expected})
  61. tm.assert_frame_equal(chg, edf)
  62. @pytest.mark.parametrize(
  63. "freq, periods, fill_method, limit",
  64. [
  65. ("5B", 5, None, None),
  66. ("3B", 3, None, None),
  67. ("3B", 3, "bfill", None),
  68. ("7B", 7, "pad", 1),
  69. ("7B", 7, "bfill", 3),
  70. ("14B", 14, None, None),
  71. ],
  72. )
  73. def test_pct_change_periods_freq(
  74. self, datetime_frame, freq, periods, fill_method, limit
  75. ):
  76. # GH#7292
  77. rs_freq = datetime_frame.pct_change(
  78. freq=freq, fill_method=fill_method, limit=limit
  79. )
  80. rs_periods = datetime_frame.pct_change(
  81. periods, fill_method=fill_method, limit=limit
  82. )
  83. tm.assert_frame_equal(rs_freq, rs_periods)
  84. empty_ts = DataFrame(index=datetime_frame.index, columns=datetime_frame.columns)
  85. rs_freq = empty_ts.pct_change(freq=freq, fill_method=fill_method, limit=limit)
  86. rs_periods = empty_ts.pct_change(periods, fill_method=fill_method, limit=limit)
  87. tm.assert_frame_equal(rs_freq, rs_periods)
  88. @pytest.mark.parametrize("fill_method", ["pad", "ffill", None])
  89. def test_pct_change_with_duplicated_indices(fill_method):
  90. # GH30463
  91. data = DataFrame(
  92. {0: [np.nan, 1, 2, 3, 9, 18], 1: [0, 1, np.nan, 3, 9, 18]}, index=["a", "b"] * 3
  93. )
  94. result = data.pct_change(fill_method=fill_method)
  95. if fill_method is None:
  96. second_column = [np.nan, np.inf, np.nan, np.nan, 2.0, 1.0]
  97. else:
  98. second_column = [np.nan, np.inf, 0.0, 2.0, 2.0, 1.0]
  99. expected = DataFrame(
  100. {0: [np.nan, np.nan, 1.0, 0.5, 2.0, 1.0], 1: second_column},
  101. index=["a", "b"] * 3,
  102. )
  103. tm.assert_frame_equal(result, expected)