test_series.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. from operator import methodcaller
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. from pandas import (
  6. MultiIndex,
  7. Series,
  8. date_range,
  9. )
  10. import pandas._testing as tm
  11. class TestSeries:
  12. @pytest.mark.parametrize("func", ["rename_axis", "_set_axis_name"])
  13. def test_set_axis_name_mi(self, func):
  14. ser = Series(
  15. [11, 21, 31],
  16. index=MultiIndex.from_tuples(
  17. [("A", x) for x in ["a", "B", "c"]], names=["l1", "l2"]
  18. ),
  19. )
  20. result = methodcaller(func, ["L1", "L2"])(ser)
  21. assert ser.index.name is None
  22. assert ser.index.names == ["l1", "l2"]
  23. assert result.index.name is None
  24. assert result.index.names, ["L1", "L2"]
  25. def test_set_axis_name_raises(self):
  26. ser = Series([1])
  27. msg = "No axis named 1 for object type Series"
  28. with pytest.raises(ValueError, match=msg):
  29. ser._set_axis_name(name="a", axis=1)
  30. def test_get_bool_data_preserve_dtype(self):
  31. ser = Series([True, False, True])
  32. result = ser._get_bool_data()
  33. tm.assert_series_equal(result, ser)
  34. def test_nonzero_single_element(self):
  35. # allow single item via bool method
  36. msg_warn = (
  37. "Series.bool is now deprecated and will be removed "
  38. "in future version of pandas"
  39. )
  40. ser = Series([True])
  41. ser1 = Series([False])
  42. with tm.assert_produces_warning(FutureWarning, match=msg_warn):
  43. assert ser.bool()
  44. with tm.assert_produces_warning(FutureWarning, match=msg_warn):
  45. assert not ser1.bool()
  46. @pytest.mark.parametrize("data", [np.nan, pd.NaT, True, False])
  47. def test_nonzero_single_element_raise_1(self, data):
  48. # single item nan to raise
  49. series = Series([data])
  50. msg = "The truth value of a Series is ambiguous"
  51. with pytest.raises(ValueError, match=msg):
  52. bool(series)
  53. @pytest.mark.parametrize("data", [np.nan, pd.NaT])
  54. def test_nonzero_single_element_raise_2(self, data):
  55. msg_warn = (
  56. "Series.bool is now deprecated and will be removed "
  57. "in future version of pandas"
  58. )
  59. msg_err = "bool cannot act on a non-boolean single element Series"
  60. series = Series([data])
  61. with tm.assert_produces_warning(FutureWarning, match=msg_warn):
  62. with pytest.raises(ValueError, match=msg_err):
  63. series.bool()
  64. @pytest.mark.parametrize("data", [(True, True), (False, False)])
  65. def test_nonzero_multiple_element_raise(self, data):
  66. # multiple bool are still an error
  67. msg_warn = (
  68. "Series.bool is now deprecated and will be removed "
  69. "in future version of pandas"
  70. )
  71. msg_err = "The truth value of a Series is ambiguous"
  72. series = Series([data])
  73. with pytest.raises(ValueError, match=msg_err):
  74. bool(series)
  75. with tm.assert_produces_warning(FutureWarning, match=msg_warn):
  76. with pytest.raises(ValueError, match=msg_err):
  77. series.bool()
  78. @pytest.mark.parametrize("data", [1, 0, "a", 0.0])
  79. def test_nonbool_single_element_raise(self, data):
  80. # single non-bool are an error
  81. msg_warn = (
  82. "Series.bool is now deprecated and will be removed "
  83. "in future version of pandas"
  84. )
  85. msg_err1 = "The truth value of a Series is ambiguous"
  86. msg_err2 = "bool cannot act on a non-boolean single element Series"
  87. series = Series([data])
  88. with pytest.raises(ValueError, match=msg_err1):
  89. bool(series)
  90. with tm.assert_produces_warning(FutureWarning, match=msg_warn):
  91. with pytest.raises(ValueError, match=msg_err2):
  92. series.bool()
  93. def test_metadata_propagation_indiv_resample(self):
  94. # resample
  95. ts = Series(
  96. np.random.default_rng(2).random(1000),
  97. index=date_range("20130101", periods=1000, freq="s"),
  98. name="foo",
  99. )
  100. result = ts.resample("1min").mean()
  101. tm.assert_metadata_equivalent(ts, result)
  102. result = ts.resample("1min").min()
  103. tm.assert_metadata_equivalent(ts, result)
  104. result = ts.resample("1min").apply(lambda x: x.sum())
  105. tm.assert_metadata_equivalent(ts, result)
  106. def test_metadata_propagation_indiv(self, monkeypatch):
  107. # check that the metadata matches up on the resulting ops
  108. ser = Series(range(3), range(3))
  109. ser.name = "foo"
  110. ser2 = Series(range(3), range(3))
  111. ser2.name = "bar"
  112. result = ser.T
  113. tm.assert_metadata_equivalent(ser, result)
  114. def finalize(self, other, method=None, **kwargs):
  115. for name in self._metadata:
  116. if method == "concat" and name == "filename":
  117. value = "+".join(
  118. [
  119. getattr(obj, name)
  120. for obj in other.objs
  121. if getattr(obj, name, None)
  122. ]
  123. )
  124. object.__setattr__(self, name, value)
  125. else:
  126. object.__setattr__(self, name, getattr(other, name, None))
  127. return self
  128. with monkeypatch.context() as m:
  129. m.setattr(Series, "_metadata", ["name", "filename"])
  130. m.setattr(Series, "__finalize__", finalize)
  131. ser.filename = "foo"
  132. ser2.filename = "bar"
  133. result = pd.concat([ser, ser2])
  134. assert result.filename == "foo+bar"
  135. assert result.name is None