casting.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import numpy as np
  2. import pytest
  3. import pandas.util._test_decorators as td
  4. import pandas as pd
  5. import pandas._testing as tm
  6. from pandas.core.internals.blocks import NumpyBlock
  7. class BaseCastingTests:
  8. """Casting to and from ExtensionDtypes"""
  9. def test_astype_object_series(self, all_data):
  10. ser = pd.Series(all_data, name="A")
  11. result = ser.astype(object)
  12. assert result.dtype == np.dtype(object)
  13. if hasattr(result._mgr, "blocks"):
  14. blk = result._mgr.blocks[0]
  15. assert isinstance(blk, NumpyBlock)
  16. assert blk.is_object
  17. assert isinstance(result._mgr.array, np.ndarray)
  18. assert result._mgr.array.dtype == np.dtype(object)
  19. def test_astype_object_frame(self, all_data):
  20. df = pd.DataFrame({"A": all_data})
  21. result = df.astype(object)
  22. if hasattr(result._mgr, "blocks"):
  23. blk = result._mgr.blocks[0]
  24. assert isinstance(blk, NumpyBlock), type(blk)
  25. assert blk.is_object
  26. assert isinstance(result._mgr.arrays[0], np.ndarray)
  27. assert result._mgr.arrays[0].dtype == np.dtype(object)
  28. # check that we can compare the dtypes
  29. comp = result.dtypes == df.dtypes
  30. assert not comp.any()
  31. def test_tolist(self, data):
  32. result = pd.Series(data).tolist()
  33. expected = list(data)
  34. assert result == expected
  35. def test_astype_str(self, data):
  36. result = pd.Series(data[:2]).astype(str)
  37. expected = pd.Series([str(x) for x in data[:2]], dtype=str)
  38. tm.assert_series_equal(result, expected)
  39. @pytest.mark.parametrize(
  40. "nullable_string_dtype",
  41. [
  42. "string[python]",
  43. pytest.param("string[pyarrow]", marks=td.skip_if_no("pyarrow")),
  44. ],
  45. )
  46. def test_astype_string(self, data, nullable_string_dtype):
  47. # GH-33465, GH#45326 as of 2.0 we decode bytes instead of calling str(obj)
  48. result = pd.Series(data[:5]).astype(nullable_string_dtype)
  49. expected = pd.Series(
  50. [str(x) if not isinstance(x, bytes) else x.decode() for x in data[:5]],
  51. dtype=nullable_string_dtype,
  52. )
  53. tm.assert_series_equal(result, expected)
  54. def test_to_numpy(self, data):
  55. expected = np.asarray(data)
  56. result = data.to_numpy()
  57. tm.assert_equal(result, expected)
  58. result = pd.Series(data).to_numpy()
  59. tm.assert_equal(result, expected)
  60. def test_astype_empty_dataframe(self, dtype):
  61. # https://github.com/pandas-dev/pandas/issues/33113
  62. df = pd.DataFrame()
  63. result = df.astype(dtype)
  64. tm.assert_frame_equal(result, df)
  65. @pytest.mark.parametrize("copy", [True, False])
  66. def test_astype_own_type(self, data, copy):
  67. # ensure that astype returns the original object for equal dtype and copy=False
  68. # https://github.com/pandas-dev/pandas/issues/28488
  69. result = data.astype(data.dtype, copy=copy)
  70. assert (result is data) is (not copy)
  71. tm.assert_extension_array_equal(result, data)