test_update.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import numpy as np
  2. import pytest
  3. import pandas.util._test_decorators as td
  4. from pandas import (
  5. CategoricalDtype,
  6. DataFrame,
  7. NaT,
  8. Series,
  9. Timestamp,
  10. )
  11. import pandas._testing as tm
  12. class TestUpdate:
  13. def test_update(self, using_copy_on_write):
  14. s = Series([1.5, np.nan, 3.0, 4.0, np.nan])
  15. s2 = Series([np.nan, 3.5, np.nan, 5.0])
  16. s.update(s2)
  17. expected = Series([1.5, 3.5, 3.0, 5.0, np.nan])
  18. tm.assert_series_equal(s, expected)
  19. # GH 3217
  20. df = DataFrame([{"a": 1}, {"a": 3, "b": 2}])
  21. df["c"] = np.nan
  22. df_orig = df.copy()
  23. df["c"].update(Series(["foo"], index=[0]))
  24. if using_copy_on_write:
  25. expected = df_orig
  26. else:
  27. expected = DataFrame(
  28. [[1, np.nan, "foo"], [3, 2.0, np.nan]], columns=["a", "b", "c"]
  29. )
  30. tm.assert_frame_equal(df, expected)
  31. @pytest.mark.parametrize(
  32. "other, dtype, expected",
  33. [
  34. # other is int
  35. ([61, 63], "int32", Series([10, 61, 12], dtype="int32")),
  36. ([61, 63], "int64", Series([10, 61, 12])),
  37. ([61, 63], float, Series([10.0, 61.0, 12.0])),
  38. ([61, 63], object, Series([10, 61, 12], dtype=object)),
  39. # other is float, but can be cast to int
  40. ([61.0, 63.0], "int32", Series([10, 61, 12], dtype="int32")),
  41. ([61.0, 63.0], "int64", Series([10, 61, 12])),
  42. ([61.0, 63.0], float, Series([10.0, 61.0, 12.0])),
  43. ([61.0, 63.0], object, Series([10, 61.0, 12], dtype=object)),
  44. # others is float, cannot be cast to int
  45. ([61.1, 63.1], "int32", Series([10.0, 61.1, 12.0])),
  46. ([61.1, 63.1], "int64", Series([10.0, 61.1, 12.0])),
  47. ([61.1, 63.1], float, Series([10.0, 61.1, 12.0])),
  48. ([61.1, 63.1], object, Series([10, 61.1, 12], dtype=object)),
  49. # other is object, cannot be cast
  50. ([(61,), (63,)], "int32", Series([10, (61,), 12])),
  51. ([(61,), (63,)], "int64", Series([10, (61,), 12])),
  52. ([(61,), (63,)], float, Series([10.0, (61,), 12.0])),
  53. ([(61,), (63,)], object, Series([10, (61,), 12])),
  54. ],
  55. )
  56. def test_update_dtypes(self, other, dtype, expected):
  57. ser = Series([10, 11, 12], dtype=dtype)
  58. other = Series(other, index=[1, 3])
  59. ser.update(other)
  60. tm.assert_series_equal(ser, expected)
  61. @pytest.mark.parametrize(
  62. "series, other, expected",
  63. [
  64. # update by key
  65. (
  66. Series({"a": 1, "b": 2, "c": 3, "d": 4}),
  67. {"b": 5, "c": np.nan},
  68. Series({"a": 1, "b": 5, "c": 3, "d": 4}),
  69. ),
  70. # update by position
  71. (Series([1, 2, 3, 4]), [np.nan, 5, 1], Series([1, 5, 1, 4])),
  72. ],
  73. )
  74. def test_update_from_non_series(self, series, other, expected):
  75. # GH 33215
  76. series.update(other)
  77. tm.assert_series_equal(series, expected)
  78. @pytest.mark.parametrize(
  79. "data, other, expected, dtype",
  80. [
  81. (["a", None], [None, "b"], ["a", "b"], "string[python]"),
  82. pytest.param(
  83. ["a", None],
  84. [None, "b"],
  85. ["a", "b"],
  86. "string[pyarrow]",
  87. marks=td.skip_if_no("pyarrow"),
  88. ),
  89. ([1, None], [None, 2], [1, 2], "Int64"),
  90. ([True, None], [None, False], [True, False], "boolean"),
  91. (
  92. ["a", None],
  93. [None, "b"],
  94. ["a", "b"],
  95. CategoricalDtype(categories=["a", "b"]),
  96. ),
  97. (
  98. [Timestamp(year=2020, month=1, day=1, tz="Europe/London"), NaT],
  99. [NaT, Timestamp(year=2020, month=1, day=1, tz="Europe/London")],
  100. [Timestamp(year=2020, month=1, day=1, tz="Europe/London")] * 2,
  101. "datetime64[ns, Europe/London]",
  102. ),
  103. ],
  104. )
  105. def test_update_extension_array_series(self, data, other, expected, dtype):
  106. result = Series(data, dtype=dtype)
  107. other = Series(other, dtype=dtype)
  108. expected = Series(expected, dtype=dtype)
  109. result.update(other)
  110. tm.assert_series_equal(result, expected)
  111. def test_update_with_categorical_type(self):
  112. # GH 25744
  113. dtype = CategoricalDtype(["a", "b", "c", "d"])
  114. s1 = Series(["a", "b", "c"], index=[1, 2, 3], dtype=dtype)
  115. s2 = Series(["b", "a"], index=[1, 2], dtype=dtype)
  116. s1.update(s2)
  117. result = s1
  118. expected = Series(["b", "a", "c"], index=[1, 2, 3], dtype=dtype)
  119. tm.assert_series_equal(result, expected)