Skip to content

Commit 52ad05a

Browse files
author
Mateusz
committed
Test column addition using .at with different values
GH30649
1 parent c90294d commit 52ad05a

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

pandas/tests/indexing/test_at.py

+62-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
import pandas._testing as tm
1717

1818

19-
def test_at_timezone():
20-
# https://github.com/pandas-dev/pandas/issues/33544
21-
result = DataFrame({"foo": [datetime(2000, 1, 1)]})
22-
result.at[0, "foo"] = datetime(2000, 1, 2, tzinfo=timezone.utc)
23-
expected = DataFrame(
24-
{"foo": [datetime(2000, 1, 2, tzinfo=timezone.utc)]}, dtype=object
25-
)
26-
tm.assert_frame_equal(result, expected)
27-
28-
2919
def test_selection_methods_of_assigned_col():
3020
# GH 29282
3121
df = DataFrame(data={"a": [1, 2, 3], "b": [4, 5, 6]})
@@ -47,6 +37,68 @@ def test_selection_methods_of_assigned_col():
4737
tm.assert_frame_equal(result, expected)
4838

4939

40+
@pytest.mark.parametrize(
41+
"value_to_assign",
42+
[
43+
"a",
44+
1,
45+
1.0,
46+
np.nan,
47+
True,
48+
("a",),
49+
["a"],
50+
datetime(2000, 1, 2, tzinfo=timezone.utc),
51+
],
52+
)
53+
def test_at_setitem_expansion__df(value_to_assign):
54+
# GH33544
55+
# GH30649
56+
result = DataFrame({0: [1, 2]}, index=[3, 2])
57+
58+
result.at[1, 1] = value_to_assign
59+
expected_result = DataFrame(
60+
{0: [1, 2, np.nan], 1: [np.nan, np.nan, value_to_assign]}, index=[3, 2, 1]
61+
)
62+
if isinstance(value_to_assign, datetime):
63+
expected_result = expected_result.astype({1: object})
64+
65+
tm.assert_frame_equal(result, expected_result)
66+
67+
68+
@pytest.mark.parametrize(
69+
"value_to_assign",
70+
[
71+
"a",
72+
1,
73+
1.0,
74+
np.nan,
75+
True,
76+
("a",),
77+
["a"],
78+
{"a": "b"},
79+
datetime(2000, 1, 2, tzinfo=timezone.utc),
80+
np.array([1]),
81+
Series([1]),
82+
],
83+
)
84+
@pytest.mark.filterwarnings("ignore::FutureWarning")
85+
def test_at_setitem_expansion__series(value_to_assign):
86+
# GH33544
87+
# GH30649
88+
result = Series([1, 2], index=[3, 2])
89+
90+
result.at[1] = value_to_assign
91+
# TODO remove if statement when concatenating bool-dtype and
92+
# numeric-dtype is cast to object
93+
if isinstance(value_to_assign, bool):
94+
result = result.astype(object)
95+
expected_result = Series([1, 2, value_to_assign], index=[3, 2, 1])
96+
if isinstance(value_to_assign, datetime):
97+
expected_result = expected_result.astype(object)
98+
99+
tm.assert_series_equal(result, expected_result)
100+
101+
50102
class TestAtSetItem:
51103
def test_at_setitem_item_cache_cleared(self):
52104
# GH#22372 Note the multi-step construction is necessary to trigger

0 commit comments

Comments
 (0)