test_api.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import pandas as pd
  4. from pandas import api
  5. from pandas.util import testing as tm
  6. class Base(object):
  7. def check(self, namespace, expected, ignored=None):
  8. # see which names are in the namespace, minus optional
  9. # ignored ones
  10. # compare vs the expected
  11. result = sorted(f for f in dir(namespace) if not f.startswith('_'))
  12. if ignored is not None:
  13. result = sorted(list(set(result) - set(ignored)))
  14. expected = sorted(expected)
  15. tm.assert_almost_equal(result, expected)
  16. class TestPDApi(Base):
  17. # these are optionally imported based on testing
  18. # & need to be ignored
  19. ignored = ['tests', 'locale', 'conftest']
  20. # top-level sub-packages
  21. lib = ['api', 'arrays', 'compat', 'core', 'errors', 'pandas',
  22. 'plotting', 'test', 'testing', 'tseries',
  23. 'util', 'options', 'io']
  24. # these are already deprecated; awaiting removal
  25. deprecated_modules = []
  26. # misc
  27. misc = ['IndexSlice', 'NaT']
  28. # top-level classes
  29. classes = ['Categorical', 'CategoricalIndex', 'DataFrame', 'DateOffset',
  30. 'DatetimeIndex', 'ExcelFile', 'ExcelWriter', 'Float64Index',
  31. 'Grouper', 'HDFStore', 'Index', 'Int64Index', 'MultiIndex',
  32. 'Period', 'PeriodIndex', 'RangeIndex', 'UInt64Index',
  33. 'Series', 'SparseArray', 'SparseDataFrame', 'SparseDtype',
  34. 'SparseSeries', 'Timedelta',
  35. 'TimedeltaIndex', 'Timestamp', 'Interval', 'IntervalIndex',
  36. 'CategoricalDtype', 'PeriodDtype', 'IntervalDtype',
  37. 'DatetimeTZDtype',
  38. 'Int8Dtype', 'Int16Dtype', 'Int32Dtype', 'Int64Dtype',
  39. 'UInt8Dtype', 'UInt16Dtype', 'UInt32Dtype', 'UInt64Dtype',
  40. ]
  41. # these are already deprecated; awaiting removal
  42. deprecated_classes = ['TimeGrouper']
  43. # these should be deprecated in the future
  44. deprecated_classes_in_future = ['Panel']
  45. # external modules exposed in pandas namespace
  46. modules = ['np', 'datetime']
  47. # top-level functions
  48. funcs = ['array', 'bdate_range', 'concat', 'crosstab', 'cut',
  49. 'date_range', 'interval_range', 'eval',
  50. 'factorize', 'get_dummies',
  51. 'infer_freq', 'isna', 'isnull', 'lreshape',
  52. 'melt', 'notna', 'notnull', 'offsets',
  53. 'merge', 'merge_ordered', 'merge_asof',
  54. 'period_range',
  55. 'pivot', 'pivot_table', 'qcut',
  56. 'show_versions', 'timedelta_range', 'unique',
  57. 'value_counts', 'wide_to_long']
  58. # top-level option funcs
  59. funcs_option = ['reset_option', 'describe_option', 'get_option',
  60. 'option_context', 'set_option',
  61. 'set_eng_float_format']
  62. # top-level read_* funcs
  63. funcs_read = ['read_clipboard', 'read_csv', 'read_excel', 'read_fwf',
  64. 'read_gbq', 'read_hdf', 'read_html', 'read_json',
  65. 'read_msgpack', 'read_pickle', 'read_sas', 'read_sql',
  66. 'read_sql_query', 'read_sql_table', 'read_stata',
  67. 'read_table', 'read_feather', 'read_parquet']
  68. # top-level to_* funcs
  69. funcs_to = ['to_datetime', 'to_msgpack',
  70. 'to_numeric', 'to_pickle', 'to_timedelta']
  71. # top-level to deprecate in the future
  72. deprecated_funcs_in_future = []
  73. # these are already deprecated; awaiting removal
  74. deprecated_funcs = []
  75. def test_api(self):
  76. self.check(pd,
  77. self.lib + self.misc +
  78. self.modules + self.deprecated_modules +
  79. self.classes + self.deprecated_classes +
  80. self.deprecated_classes_in_future +
  81. self.funcs + self.funcs_option +
  82. self.funcs_read + self.funcs_to +
  83. self.deprecated_funcs_in_future +
  84. self.deprecated_funcs,
  85. self.ignored)
  86. class TestApi(Base):
  87. allowed = ['types', 'extensions']
  88. def test_api(self):
  89. self.check(api, self.allowed)
  90. class TestTesting(Base):
  91. funcs = ['assert_frame_equal', 'assert_series_equal',
  92. 'assert_index_equal']
  93. def test_testing(self):
  94. from pandas import testing
  95. self.check(testing, self.funcs)
  96. class TestTopLevelDeprecations(object):
  97. # top-level API deprecations
  98. # GH 13790
  99. def test_TimeGrouper(self):
  100. with tm.assert_produces_warning(FutureWarning,
  101. check_stacklevel=False):
  102. pd.TimeGrouper(freq='D')
  103. class TestCDateRange(object):
  104. def test_deprecation_cdaterange(self):
  105. # GH17596
  106. from pandas.core.indexes.datetimes import cdate_range
  107. with tm.assert_produces_warning(FutureWarning,
  108. check_stacklevel=False):
  109. cdate_range('2017-01-01', '2017-12-31')
  110. class TestCategoricalMove(object):
  111. def test_categorical_move(self):
  112. # May have been cached by another import, e.g. pickle tests.
  113. sys.modules.pop("pandas.core.categorical", None)
  114. with tm.assert_produces_warning(FutureWarning):
  115. from pandas.core.categorical import Categorical # noqa
  116. sys.modules.pop("pandas.core.categorical", None)
  117. with tm.assert_produces_warning(FutureWarning):
  118. from pandas.core.categorical import CategoricalDtype # noqa