interface.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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.util.testing as tm
  6. from .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_ndim(self, data):
  15. assert data.ndim == 1
  16. def test_can_hold_na_valid(self, data):
  17. # GH-20761
  18. assert data._can_hold_na is True
  19. def test_memory_usage(self, data):
  20. s = pd.Series(data)
  21. result = s.memory_usage(index=False)
  22. assert result == s.nbytes
  23. def test_array_interface(self, data):
  24. result = np.array(data)
  25. assert result[0] == data[0]
  26. result = np.array(data, dtype=object)
  27. expected = np.array(list(data), dtype=object)
  28. tm.assert_numpy_array_equal(result, expected)
  29. def test_is_extension_array_dtype(self, data):
  30. assert is_extension_array_dtype(data)
  31. assert is_extension_array_dtype(data.dtype)
  32. assert is_extension_array_dtype(pd.Series(data))
  33. assert isinstance(data.dtype, ExtensionDtype)
  34. def test_no_values_attribute(self, data):
  35. # GH-20735: EA's with .values attribute give problems with internal
  36. # code, disallowing this for now until solved
  37. assert not hasattr(data, 'values')
  38. assert not hasattr(data, '_values')
  39. def test_is_numeric_honored(self, data):
  40. result = pd.Series(data)
  41. assert result._data.blocks[0].is_numeric is data.dtype._is_numeric
  42. def test_isna_extension_array(self, data_missing):
  43. # If your `isna` returns an ExtensionArray, you must also implement
  44. # _reduce. At the *very* least, you must implement any and all
  45. na = data_missing.isna()
  46. if is_extension_array_dtype(na):
  47. assert na._reduce('any')
  48. assert na.any()
  49. assert not na._reduce('all')
  50. assert not na.all()
  51. assert na.dtype._is_boolean