Skip to content

Commit 1ac599c

Browse files
authored
TST: Test column names underscore (#52738)
* Added new tests to check that columns starting with underscore works with to_dict * blacked * implemented asset for the whole dictionnary
1 parent 71e969f commit 1ac599c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/tests/frame/test_constructors.py

+18
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ def test_constructor_from_tzaware_datetimeindex(self):
135135
result = Series(idx)
136136
tm.assert_series_equal(result, expected)
137137

138+
def test_columns_with_leading_underscore_work_with_to_dict(self):
139+
col_underscore = "_b"
140+
df = DataFrame({"a": [1, 2], col_underscore: [3, 4]})
141+
d = df.to_dict(orient="records")
142+
143+
ref_d = [{"a": 1, col_underscore: 3}, {"a": 2, col_underscore: 4}]
144+
145+
assert ref_d == d
146+
147+
def test_columns_with_leading_number_and_underscore_work_with_to_dict(self):
148+
col_with_num = "1_b"
149+
df = DataFrame({"a": [1, 2], col_with_num: [3, 4]})
150+
d = df.to_dict(orient="records")
151+
152+
ref_d = [{"a": 1, col_with_num: 3}, {"a": 2, col_with_num: 4}]
153+
154+
assert ref_d == d
155+
138156
def test_array_of_dt64_nat_with_td64dtype_raises(self, frame_or_series):
139157
# GH#39462
140158
nat = np.datetime64("NaT", "ns")

0 commit comments

Comments
 (0)