|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | +from pandas import api |
| 10 | +import pandas._testing as tm |
| 11 | + |
| 12 | + |
| 13 | +class Base: |
| 14 | + def check(self, namespace, expected, ignored=None): |
| 15 | + # see which names are in the namespace, minus optional |
| 16 | + # ignored ones |
| 17 | + # compare vs the expected |
| 18 | + |
| 19 | + result = sorted(f for f in dir(namespace) if not f.startswith("__")) |
| 20 | + if ignored is not None: |
| 21 | + result = sorted(set(result) - set(ignored)) |
| 22 | + |
| 23 | + expected = sorted(expected) |
| 24 | + tm.assert_almost_equal(result, expected) |
| 25 | + |
| 26 | + |
| 27 | +class TestPDApi(Base): |
| 28 | + # these are optionally imported based on testing |
| 29 | + # & need to be ignored |
| 30 | + ignored = ["tests", "locale", "conftest"] |
| 31 | + |
| 32 | + # top-level sub-packages |
| 33 | + public_lib = [ |
| 34 | + "api", |
| 35 | + "arrays", |
| 36 | + "options", |
| 37 | + "test", |
| 38 | + "testing", |
| 39 | + "errors", |
| 40 | + "plotting", |
| 41 | + "io", |
| 42 | + "tseries", |
| 43 | + ] |
| 44 | + private_lib = ["compat", "core", "pandas", "util"] |
| 45 | + |
| 46 | + # these are already deprecated; awaiting removal |
| 47 | + deprecated_modules: list[str] = ["np", "datetime"] |
| 48 | + |
| 49 | + # misc |
| 50 | + misc = ["IndexSlice", "NaT", "NA"] |
| 51 | + |
| 52 | + # top-level classes |
| 53 | + classes = [ |
| 54 | + "Categorical", |
| 55 | + "CategoricalIndex", |
| 56 | + "DataFrame", |
| 57 | + "DateOffset", |
| 58 | + "DatetimeIndex", |
| 59 | + "ExcelFile", |
| 60 | + "ExcelWriter", |
| 61 | + "Float64Index", |
| 62 | + "Flags", |
| 63 | + "Grouper", |
| 64 | + "HDFStore", |
| 65 | + "Index", |
| 66 | + "Int64Index", |
| 67 | + "MultiIndex", |
| 68 | + "Period", |
| 69 | + "PeriodIndex", |
| 70 | + "RangeIndex", |
| 71 | + "UInt64Index", |
| 72 | + "Series", |
| 73 | + "SparseDtype", |
| 74 | + "StringDtype", |
| 75 | + "Timedelta", |
| 76 | + "TimedeltaIndex", |
| 77 | + "Timestamp", |
| 78 | + "Interval", |
| 79 | + "IntervalIndex", |
| 80 | + "CategoricalDtype", |
| 81 | + "PeriodDtype", |
| 82 | + "IntervalDtype", |
| 83 | + "DatetimeTZDtype", |
| 84 | + "BooleanDtype", |
| 85 | + "Int8Dtype", |
| 86 | + "Int16Dtype", |
| 87 | + "Int32Dtype", |
| 88 | + "Int64Dtype", |
| 89 | + "UInt8Dtype", |
| 90 | + "UInt16Dtype", |
| 91 | + "UInt32Dtype", |
| 92 | + "UInt64Dtype", |
| 93 | + "Float32Dtype", |
| 94 | + "Float64Dtype", |
| 95 | + "NamedAgg", |
| 96 | + ] |
| 97 | + |
| 98 | + # these are already deprecated; awaiting removal |
| 99 | + deprecated_classes: list[str] = ["Float64Index", "Int64Index", "UInt64Index"] |
| 100 | + |
| 101 | + # these should be deprecated in the future |
| 102 | + deprecated_classes_in_future: list[str] = ["SparseArray"] |
| 103 | + |
| 104 | + # external modules exposed in pandas namespace |
| 105 | + modules: list[str] = [] |
| 106 | + |
| 107 | + # top-level functions |
| 108 | + funcs = [ |
| 109 | + "array", |
| 110 | + "bdate_range", |
| 111 | + "concat", |
| 112 | + "crosstab", |
| 113 | + "cut", |
| 114 | + "date_range", |
| 115 | + "interval_range", |
| 116 | + "eval", |
| 117 | + "factorize", |
| 118 | + "get_dummies", |
| 119 | + "infer_freq", |
| 120 | + "isna", |
| 121 | + "isnull", |
| 122 | + "lreshape", |
| 123 | + "melt", |
| 124 | + "notna", |
| 125 | + "notnull", |
| 126 | + "offsets", |
| 127 | + "merge", |
| 128 | + "merge_ordered", |
| 129 | + "merge_asof", |
| 130 | + "period_range", |
| 131 | + "pivot", |
| 132 | + "pivot_table", |
| 133 | + "qcut", |
| 134 | + "show_versions", |
| 135 | + "timedelta_range", |
| 136 | + "unique", |
| 137 | + "value_counts", |
| 138 | + "wide_to_long", |
| 139 | + ] |
| 140 | + |
| 141 | + # top-level option funcs |
| 142 | + funcs_option = [ |
| 143 | + "reset_option", |
| 144 | + "describe_option", |
| 145 | + "get_option", |
| 146 | + "option_context", |
| 147 | + "set_option", |
| 148 | + "set_eng_float_format", |
| 149 | + ] |
| 150 | + |
| 151 | + # top-level read_* funcs |
| 152 | + funcs_read = [ |
| 153 | + "read_clipboard", |
| 154 | + "read_csv", |
| 155 | + "read_excel", |
| 156 | + "read_fwf", |
| 157 | + "read_gbq", |
| 158 | + "read_hdf", |
| 159 | + "read_html", |
| 160 | + "read_xml", |
| 161 | + "read_json", |
| 162 | + "read_pickle", |
| 163 | + "read_sas", |
| 164 | + "read_sql", |
| 165 | + "read_sql_query", |
| 166 | + "read_sql_table", |
| 167 | + "read_stata", |
| 168 | + "read_table", |
| 169 | + "read_feather", |
| 170 | + "read_parquet", |
| 171 | + "read_orc", |
| 172 | + "read_spss", |
| 173 | + ] |
| 174 | + |
| 175 | + # top-level json funcs |
| 176 | + funcs_json = ["json_normalize"] |
| 177 | + |
| 178 | + # top-level to_* funcs |
| 179 | + funcs_to = ["to_datetime", "to_numeric", "to_pickle", "to_timedelta"] |
| 180 | + |
| 181 | + # top-level to deprecate in the future |
| 182 | + deprecated_funcs_in_future: list[str] = [] |
| 183 | + |
| 184 | + # these are already deprecated; awaiting removal |
| 185 | + deprecated_funcs: list[str] = [] |
| 186 | + |
| 187 | + # private modules in pandas namespace |
| 188 | + private_modules = [ |
| 189 | + "_config", |
| 190 | + "_libs", |
| 191 | + "_is_numpy_dev", |
| 192 | + "_testing", |
| 193 | + "_typing", |
| 194 | + "_version", |
| 195 | + ] |
| 196 | + |
| 197 | + def test_api(self): |
| 198 | + |
| 199 | + checkthese = ( |
| 200 | + self.public_lib |
| 201 | + + self.private_lib |
| 202 | + + self.misc |
| 203 | + + self.modules |
| 204 | + + self.classes |
| 205 | + + self.funcs |
| 206 | + + self.funcs_option |
| 207 | + + self.funcs_read |
| 208 | + + self.funcs_json |
| 209 | + + self.funcs_to |
| 210 | + + self.private_modules |
| 211 | + ) |
| 212 | + self.check(namespace=pd, expected=checkthese, ignored=self.ignored) |
| 213 | + |
| 214 | + def test_api_all(self): |
| 215 | + expected = set( |
| 216 | + self.public_lib |
| 217 | + + self.misc |
| 218 | + + self.modules |
| 219 | + + self.classes |
| 220 | + + self.funcs |
| 221 | + + self.funcs_option |
| 222 | + + self.funcs_read |
| 223 | + + self.funcs_json |
| 224 | + + self.funcs_to |
| 225 | + ) - set(self.deprecated_classes) |
| 226 | + actual = set(pd.__all__) |
| 227 | + |
| 228 | + extraneous = actual - expected |
| 229 | + assert not extraneous |
| 230 | + |
| 231 | + missing = expected - actual |
| 232 | + assert not missing |
| 233 | + |
| 234 | + def test_depr(self): |
| 235 | + deprecated_list = ( |
| 236 | + self.deprecated_modules |
| 237 | + + self.deprecated_classes |
| 238 | + + self.deprecated_classes_in_future |
| 239 | + + self.deprecated_funcs |
| 240 | + + self.deprecated_funcs_in_future |
| 241 | + ) |
| 242 | + for depr in deprecated_list: |
| 243 | + with tm.assert_produces_warning(FutureWarning): |
| 244 | + _ = getattr(pd, depr) |
| 245 | + |
| 246 | + |
| 247 | +def test_datetime(): |
| 248 | + from datetime import datetime |
| 249 | + import warnings |
| 250 | + |
| 251 | + with warnings.catch_warnings(): |
| 252 | + warnings.simplefilter("ignore", FutureWarning) |
| 253 | + assert datetime(2015, 1, 2, 0, 0) == datetime(2015, 1, 2, 0, 0) |
| 254 | + |
| 255 | + assert isinstance(datetime(2015, 1, 2, 0, 0), datetime) |
| 256 | + |
| 257 | + |
| 258 | +def test_sparsearray(): |
| 259 | + import warnings |
| 260 | + |
| 261 | + with warnings.catch_warnings(): |
| 262 | + warnings.simplefilter("ignore", FutureWarning) |
| 263 | + assert isinstance(pd.array([1, 2, 3], dtype="Sparse"), pd.SparseArray) |
| 264 | + |
| 265 | + |
| 266 | +def test_np(): |
| 267 | + import warnings |
| 268 | + |
| 269 | + import numpy as np |
| 270 | + |
| 271 | + with warnings.catch_warnings(): |
| 272 | + warnings.simplefilter("ignore", FutureWarning) |
| 273 | + assert (pd.np.arange(0, 10) == np.arange(0, 10)).all() |
| 274 | + |
| 275 | + |
| 276 | +class TestApi(Base): |
| 277 | + allowed = ["types", "extensions", "indexers"] |
| 278 | + |
| 279 | + def test_api(self): |
| 280 | + self.check(api, self.allowed) |
| 281 | + |
| 282 | + |
| 283 | +class TestTesting(Base): |
| 284 | + funcs = [ |
| 285 | + "assert_frame_equal", |
| 286 | + "assert_series_equal", |
| 287 | + "assert_index_equal", |
| 288 | + "assert_extension_array_equal", |
| 289 | + ] |
| 290 | + |
| 291 | + def test_testing(self): |
| 292 | + from pandas import testing # noqa: PDF015 |
| 293 | + |
| 294 | + self.check(testing, self.funcs) |
| 295 | + |
| 296 | + def test_util_testing_deprecated(self): |
| 297 | + # avoid cache state affecting the test |
| 298 | + sys.modules.pop("pandas.util.testing", None) |
| 299 | + |
| 300 | + with tm.assert_produces_warning(FutureWarning) as m: |
| 301 | + import pandas.util.testing # noqa: F401 |
| 302 | + |
| 303 | + assert "pandas.util.testing is deprecated" in str(m[0].message) |
| 304 | + assert "pandas.testing instead" in str(m[0].message) |
| 305 | + |
| 306 | + def test_util_testing_deprecated_direct(self): |
| 307 | + # avoid cache state affecting the test |
| 308 | + sys.modules.pop("pandas.util.testing", None) |
| 309 | + with tm.assert_produces_warning(FutureWarning) as m: |
| 310 | + from pandas.util.testing import assert_series_equal # noqa: F401 |
| 311 | + |
| 312 | + assert "pandas.util.testing is deprecated" in str(m[0].message) |
| 313 | + assert "pandas.testing instead" in str(m[0].message) |
| 314 | + |
| 315 | + def test_util_in_top_level(self): |
| 316 | + # in a subprocess to avoid import caching issues |
| 317 | + out = subprocess.check_output( |
| 318 | + [ |
| 319 | + sys.executable, |
| 320 | + "-c", |
| 321 | + "import pandas; pandas.util.testing.assert_series_equal", |
| 322 | + ], |
| 323 | + stderr=subprocess.STDOUT, |
| 324 | + ).decode() |
| 325 | + assert "pandas.util.testing is deprecated" in out |
| 326 | + |
| 327 | + with pytest.raises(AttributeError, match="foo"): |
| 328 | + pd.util.foo |
0 commit comments