PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/statsmodels/tsa/filters/_utils.py

https://github.com/chatcannon/statsmodels
Python | 149 lines | 88 code | 20 blank | 41 comment | 17 complexity | 79385527c219300df1772bf230aa8d5e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from functools import wraps
  2. from statsmodels.tools.data import _is_using_pandas
  3. from statsmodels.tsa.base import datetools
  4. from statsmodels.tsa.tsatools import freq_to_period
  5. def _get_pandas_wrapper(X, trim_head=None, trim_tail=None, names=None):
  6. index = X.index
  7. #TODO: allow use index labels
  8. if trim_head is None and trim_tail is None:
  9. index = index
  10. elif trim_tail is None:
  11. index = index[trim_head:]
  12. elif trim_head is None:
  13. index = index[:-trim_tail]
  14. else:
  15. index = index[trim_head:-trim_tail]
  16. if hasattr(X, "columns"):
  17. if names is None:
  18. names = X.columns
  19. return lambda x : X.__class__(x, index=index, columns=names)
  20. else:
  21. if names is None:
  22. names = X.name
  23. return lambda x : X.__class__(x, index=index, name=names)
  24. def _maybe_get_pandas_wrapper(X, trim_head=None, trim_tail=None):
  25. """
  26. If using pandas returns a function to wrap the results, e.g., wrapper(X)
  27. trim is an integer for the symmetric truncation of the series in some
  28. filters.
  29. otherwise returns None
  30. """
  31. if _is_using_pandas(X, None):
  32. return _get_pandas_wrapper(X, trim_head, trim_tail)
  33. else:
  34. return
  35. def _maybe_get_pandas_wrapper_freq(X, trim=None):
  36. if _is_using_pandas(X, None):
  37. index = X.index
  38. func = _get_pandas_wrapper(X, trim)
  39. freq = index.inferred_freq
  40. return func, freq
  41. else:
  42. return lambda x : x, None
  43. def pandas_wrapper(func, trim_head=None, trim_tail=None, names=None, *args,
  44. **kwargs):
  45. @wraps(func)
  46. def new_func(X, *args, **kwargs):
  47. # quick pass-through for do nothing case
  48. if not _is_using_pandas(X, None):
  49. return func(X, *args, **kwargs)
  50. wrapper_func = _get_pandas_wrapper(X, trim_head, trim_tail,
  51. names)
  52. ret = func(X, *args, **kwargs)
  53. ret = wrapper_func(ret)
  54. return ret
  55. return new_func
  56. def pandas_wrapper_bunch(func, trim_head=None, trim_tail=None,
  57. names=None, *args, **kwargs):
  58. @wraps(func)
  59. def new_func(X, *args, **kwargs):
  60. # quick pass-through for do nothing case
  61. if not _is_using_pandas(X, None):
  62. return func(X, *args, **kwargs)
  63. wrapper_func = _get_pandas_wrapper(X, trim_head, trim_tail,
  64. names)
  65. ret = func(X, *args, **kwargs)
  66. ret = wrapper_func(ret)
  67. return ret
  68. return new_func
  69. def pandas_wrapper_predict(func, trim_head=None, trim_tail=None,
  70. columns=None, *args, **kwargs):
  71. pass
  72. def pandas_wrapper_freq(func, trim_head=None, trim_tail=None,
  73. freq_kw='freq', columns=None, *args, **kwargs):
  74. """
  75. Return a new function that catches the incoming X, checks if it's pandas,
  76. calls the functions as is. Then wraps the results in the incoming index.
  77. Deals with frequencies. Expects that the function returns a tuple,
  78. a Bunch object, or a pandas-object.
  79. """
  80. @wraps(func)
  81. def new_func(X, *args, **kwargs):
  82. # quick pass-through for do nothing case
  83. if not _is_using_pandas(X, None):
  84. return func(X, *args, **kwargs)
  85. wrapper_func = _get_pandas_wrapper(X, trim_head, trim_tail,
  86. columns)
  87. index = X.index
  88. freq = index.inferred_freq
  89. kwargs.update({freq_kw : freq_to_period(freq)})
  90. ret = func(X, *args, **kwargs)
  91. ret = wrapper_func(ret)
  92. return ret
  93. return new_func
  94. def dummy_func(X):
  95. return X
  96. def dummy_func_array(X):
  97. return X.values
  98. def dummy_func_pandas_columns(X):
  99. return X.values
  100. def dummy_func_pandas_series(X):
  101. return X['A']
  102. import pandas as pd
  103. import numpy as np
  104. def test_pandas_freq_decorator():
  105. X = pd.util.testing.makeDataFrame()
  106. # in X, get a function back that returns an X with the same columns
  107. func = pandas_wrapper(dummy_func)
  108. np.testing.assert_equal(func(X.values), X)
  109. func = pandas_wrapper(dummy_func_array)
  110. pd.util.testing.assert_frame_equal(func(X), X)
  111. expected = X.rename(columns=dict(zip('ABCD', 'EFGH')))
  112. func = pandas_wrapper(dummy_func_array, names=list('EFGH'))
  113. pd.util.testing.assert_frame_equal(func(X), expected)