interface.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import numpy as np
  2. from pandas.core.dtypes.common import is_extension_array_dtype
  3. from pandas.core.dtypes.dtypes import ExtensionDtype
  4. import pandas as pd
  5. import pandas._testing as tm
  6. from pandas.tests.extension.base.base import BaseExtensionTests
  7. class BaseInterfaceTests(BaseExtensionTests):
  8. """Tests that the basic interface is satisfied."""
  9. # ------------------------------------------------------------------------
  10. # Interface
  11. # ------------------------------------------------------------------------
  12. def test_len(self, data):
  13. assert len(data) == 100
  14. def test_size(self, data):
  15. assert data.size == 100
  16. def test_ndim(self, data):
  17. assert data.ndim == 1
  18. def test_can_hold_na_valid(self, data):
  19. # GH-20761
  20. assert data._can_hold_na is True
  21. def test_contains(self, data, data_missing):
  22. # GH-37867
  23. # Tests for membership checks. Membership checks for nan-likes is tricky and
  24. # the settled on rule is: `nan_like in arr` is True if nan_like is
  25. # arr.dtype.na_value and arr.isna().any() is True. Else the check returns False.
  26. na_value = data.dtype.na_value
  27. # ensure data without missing values
  28. data = data[~data.isna()]
  29. # first elements are non-missing
  30. assert data[0] in data
  31. assert data_missing[0] in data_missing
  32. # check the presence of na_value
  33. assert na_value in data_missing
  34. assert na_value not in data
  35. # the data can never contain other nan-likes than na_value
  36. for na_value_obj in tm.NULL_OBJECTS:
  37. if na_value_obj is na_value or type(na_value_obj) == type(na_value):
  38. # type check for e.g. two instances of Decimal("NAN")
  39. continue
  40. assert na_value_obj not in data
  41. assert na_value_obj not in data_missing
  42. def test_memory_usage(self, data):
  43. s = pd.Series(data)
  44. result = s.memory_usage(index=False)
  45. assert result == s.nbytes
  46. def test_array_interface(self, data):
  47. result = np.array(data)
  48. assert result[0] == data[0]
  49. result = np.array(data, dtype=object)
  50. expected = np.array(list(data), dtype=object)
  51. tm.assert_numpy_array_equal(result, expected)
  52. def test_is_extension_array_dtype(self, data):
  53. assert is_extension_array_dtype(data)
  54. assert is_extension_array_dtype(data.dtype)
  55. assert is_extension_array_dtype(pd.Series(data))
  56. assert isinstance(data.dtype, ExtensionDtype)
  57. def test_no_values_attribute(self, data):
  58. # GH-20735: EA's with .values attribute give problems with internal
  59. # code, disallowing this for now until solved
  60. assert not hasattr(data, "values")
  61. assert not hasattr(data, "_values")
  62. def test_is_numeric_honored(self, data):
  63. result = pd.Series(data)
  64. if hasattr(result._mgr, "blocks"):
  65. assert result._mgr.blocks[0].is_numeric is data.dtype._is_numeric
  66. def test_isna_extension_array(self, data_missing):
  67. # If your `isna` returns an ExtensionArray, you must also implement
  68. # _reduce. At the *very* least, you must implement any and all
  69. na = data_missing.isna()
  70. if is_extension_array_dtype(na):
  71. assert na._reduce("any")
  72. assert na.any()
  73. assert not na._reduce("all")
  74. assert not na.all()
  75. assert na.dtype._is_boolean
  76. def test_copy(self, data):
  77. # GH#27083 removing deep keyword from EA.copy
  78. assert data[0] != data[1]
  79. result = data.copy()
  80. data[1] = data[0]
  81. assert result[1] != result[0]
  82. def test_view(self, data):
  83. # view with no dtype should return a shallow copy, *not* the same
  84. # object
  85. assert data[1] != data[0]
  86. result = data.view()
  87. assert result is not data
  88. assert type(result) == type(data)
  89. result[1] = result[0]
  90. assert data[1] == data[0]
  91. # check specifically that the `dtype` kwarg is accepted
  92. data.view(dtype=None)
  93. def test_tolist(self, data):
  94. result = data.tolist()
  95. expected = list(data)
  96. assert isinstance(result, list)
  97. assert result == expected