test_series.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. ser = Series([True])
  37. assert ser.bool()
  38. ser = Series([False])
  39. assert not ser.bool()
  40. @pytest.mark.parametrize("data", [np.nan, pd.NaT, True, False])
  41. def test_nonzero_single_element_raise_1(self, data):
  42. # single item nan to raise
  43. series = Series([data])
  44. msg = "The truth value of a Series is ambiguous"
  45. with pytest.raises(ValueError, match=msg):
  46. bool(series)
  47. @pytest.mark.parametrize("data", [np.nan, pd.NaT])
  48. def test_nonzero_single_element_raise_2(self, data):
  49. series = Series([data])
  50. msg = "bool cannot act on a non-boolean single element Series"
  51. with pytest.raises(ValueError, match=msg):
  52. series.bool()
  53. @pytest.mark.parametrize("data", [(True, True), (False, False)])
  54. def test_nonzero_multiple_element_raise(self, data):
  55. # multiple bool are still an error
  56. series = Series([data])
  57. msg = "The truth value of a Series is ambiguous"
  58. with pytest.raises(ValueError, match=msg):
  59. bool(series)
  60. with pytest.raises(ValueError, match=msg):
  61. series.bool()
  62. @pytest.mark.parametrize("data", [1, 0, "a", 0.0])
  63. def test_nonbool_single_element_raise(self, data):
  64. # single non-bool are an error
  65. series = Series([data])
  66. msg = "The truth value of a Series is ambiguous"
  67. with pytest.raises(ValueError, match=msg):
  68. bool(series)
  69. msg = "bool cannot act on a non-boolean single element Series"
  70. with pytest.raises(ValueError, match=msg):
  71. series.bool()
  72. def test_metadata_propagation_indiv_resample(self):
  73. # resample
  74. ts = Series(
  75. np.random.rand(1000),
  76. index=date_range("20130101", periods=1000, freq="s"),
  77. name="foo",
  78. )
  79. result = ts.resample("1T").mean()
  80. tm.assert_metadata_equivalent(ts, result)
  81. result = ts.resample("1T").min()
  82. tm.assert_metadata_equivalent(ts, result)
  83. result = ts.resample("1T").apply(lambda x: x.sum())
  84. tm.assert_metadata_equivalent(ts, result)
  85. def test_metadata_propagation_indiv(self, monkeypatch):
  86. # check that the metadata matches up on the resulting ops
  87. ser = Series(range(3), range(3))
  88. ser.name = "foo"
  89. ser2 = Series(range(3), range(3))
  90. ser2.name = "bar"
  91. result = ser.T
  92. tm.assert_metadata_equivalent(ser, result)
  93. def finalize(self, other, method=None, **kwargs):
  94. for name in self._metadata:
  95. if method == "concat" and name == "filename":
  96. value = "+".join(
  97. [
  98. getattr(obj, name)
  99. for obj in other.objs
  100. if getattr(obj, name, None)
  101. ]
  102. )
  103. object.__setattr__(self, name, value)
  104. else:
  105. object.__setattr__(self, name, getattr(other, name, None))
  106. return self
  107. with monkeypatch.context() as m:
  108. m.setattr(Series, "_metadata", ["name", "filename"])
  109. m.setattr(Series, "__finalize__", finalize)
  110. ser.filename = "foo"
  111. ser2.filename = "bar"
  112. result = pd.concat([ser, ser2])
  113. assert result.filename == "foo+bar"
  114. assert result.name is None