test_api.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. Tests for the pseudo-public API implemented in internals/api.py and exposed
  3. in core.internals
  4. """
  5. import pytest
  6. import pandas as pd
  7. import pandas._testing as tm
  8. from pandas.core import internals
  9. from pandas.core.internals import api
  10. def test_internals_api():
  11. assert internals.make_block is api.make_block
  12. def test_namespace():
  13. # SUBJECT TO CHANGE
  14. modules = [
  15. "blocks",
  16. "concat",
  17. "managers",
  18. "construction",
  19. "array_manager",
  20. "base",
  21. "api",
  22. "ops",
  23. ]
  24. expected = [
  25. "make_block",
  26. "DataManager",
  27. "ArrayManager",
  28. "BlockManager",
  29. "SingleDataManager",
  30. "SingleBlockManager",
  31. "SingleArrayManager",
  32. "concatenate_managers",
  33. ]
  34. result = [x for x in dir(internals) if not x.startswith("__")]
  35. assert set(result) == set(expected + modules)
  36. @pytest.mark.parametrize(
  37. "name",
  38. [
  39. "NumericBlock",
  40. "ObjectBlock",
  41. "Block",
  42. "ExtensionBlock",
  43. "DatetimeTZBlock",
  44. ],
  45. )
  46. def test_deprecations(name):
  47. # GH#55139
  48. msg = f"{name} is deprecated.* Use public APIs instead"
  49. with tm.assert_produces_warning(DeprecationWarning, match=msg):
  50. getattr(internals, name)
  51. if name not in ["NumericBlock", "ObjectBlock"]:
  52. # NumericBlock and ObjectBlock are not in the internals.api namespace
  53. with tm.assert_produces_warning(DeprecationWarning, match=msg):
  54. getattr(api, name)
  55. def test_make_block_2d_with_dti():
  56. # GH#41168
  57. dti = pd.date_range("2012", periods=3, tz="UTC")
  58. blk = api.make_block(dti, placement=[0])
  59. assert blk.shape == (1, 3)
  60. assert blk.values.shape == (1, 3)
  61. def test_create_block_manager_from_blocks_deprecated():
  62. # GH#33892
  63. # If they must, downstream packages should get this from internals.api,
  64. # not internals.
  65. msg = (
  66. "create_block_manager_from_blocks is deprecated and will be "
  67. "removed in a future version. Use public APIs instead"
  68. )
  69. with tm.assert_produces_warning(DeprecationWarning, match=msg):
  70. internals.create_block_manager_from_blocks