test_infer_objects.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import numpy as np
  2. from pandas import (
  3. Series,
  4. interval_range,
  5. )
  6. import pandas._testing as tm
  7. class TestInferObjects:
  8. def test_copy(self, index_or_series):
  9. # GH#50096
  10. # case where we don't need to do inference because it is already non-object
  11. obj = index_or_series(np.array([1, 2, 3], dtype="int64"))
  12. result = obj.infer_objects(copy=False)
  13. assert tm.shares_memory(result, obj)
  14. # case where we try to do inference but can't do better than object
  15. obj2 = index_or_series(np.array(["foo", 2], dtype=object))
  16. result2 = obj2.infer_objects(copy=False)
  17. assert tm.shares_memory(result2, obj2)
  18. def test_infer_objects_series(self, index_or_series):
  19. # GH#11221
  20. actual = index_or_series(np.array([1, 2, 3], dtype="O")).infer_objects()
  21. expected = index_or_series([1, 2, 3])
  22. tm.assert_equal(actual, expected)
  23. actual = index_or_series(np.array([1, 2, 3, None], dtype="O")).infer_objects()
  24. expected = index_or_series([1.0, 2.0, 3.0, np.nan])
  25. tm.assert_equal(actual, expected)
  26. # only soft conversions, unconvertable pass thru unchanged
  27. obj = index_or_series(np.array([1, 2, 3, None, "a"], dtype="O"))
  28. actual = obj.infer_objects()
  29. expected = index_or_series([1, 2, 3, None, "a"], dtype=object)
  30. assert actual.dtype == "object"
  31. tm.assert_equal(actual, expected)
  32. def test_infer_objects_interval(self, index_or_series):
  33. # GH#50090
  34. ii = interval_range(1, 10)
  35. obj = index_or_series(ii)
  36. result = obj.astype(object).infer_objects()
  37. tm.assert_equal(result, obj)
  38. def test_infer_objects_bytes(self):
  39. # GH#49650
  40. ser = Series([b"a"], dtype="bytes")
  41. expected = ser.copy()
  42. result = ser.infer_objects()
  43. tm.assert_series_equal(result, expected)