Skip to content

Commit e734449

Browse files
authored
Deprecate Aliases as orient Argument in DataFrame.to_dict (pandas-dev#32516)
1 parent 9e9785b commit e734449

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Deprecations
206206
- Lookups on a :class:`Series` with a single-item list containing a slice (e.g. ``ser[[slice(0, 4)]]``) are deprecated, will raise in a future version. Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`)
207207
- :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`)
208208
- Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`)
209-
-
209+
- :meth:`DataFrame.to_dict` has deprecated accepting short names for ``orient`` in future versions (:issue:`32515`)
210210

211211
.. ---------------------------------------------------------------------------
212212

pandas/core/frame.py

+44-6
Original file line numberDiff line numberDiff line change
@@ -1401,11 +1401,45 @@ def to_dict(self, orient="dict", into=dict):
14011401
)
14021402
# GH16122
14031403
into_c = com.standardize_mapping(into)
1404-
if orient.lower().startswith("d"):
1404+
1405+
orient = orient.lower()
1406+
# GH32515
1407+
if orient.startswith(("d", "l", "s", "r", "i")) and orient not in {
1408+
"dict",
1409+
"list",
1410+
"series",
1411+
"split",
1412+
"records",
1413+
"index",
1414+
}:
1415+
warnings.warn(
1416+
"Using short name for 'orient' is deprecated. Only the "
1417+
"options: ('dict', list, 'series', 'split', 'records', 'index') "
1418+
"will be used in a future version. Use one of the above "
1419+
"to silence this warning.",
1420+
FutureWarning,
1421+
)
1422+
1423+
if orient.startswith("d"):
1424+
orient = "dict"
1425+
elif orient.startswith("l"):
1426+
orient = "list"
1427+
elif orient.startswith("sp"):
1428+
orient = "split"
1429+
elif orient.startswith("s"):
1430+
orient = "series"
1431+
elif orient.startswith("r"):
1432+
orient = "records"
1433+
elif orient.startswith("i"):
1434+
orient = "index"
1435+
1436+
if orient == "dict":
14051437
return into_c((k, v.to_dict(into)) for k, v in self.items())
1406-
elif orient.lower().startswith("l"):
1438+
1439+
elif orient == "list":
14071440
return into_c((k, v.tolist()) for k, v in self.items())
1408-
elif orient.lower().startswith("sp"):
1441+
1442+
elif orient == "split":
14091443
return into_c(
14101444
(
14111445
("index", self.index.tolist()),
@@ -1419,9 +1453,11 @@ def to_dict(self, orient="dict", into=dict):
14191453
),
14201454
)
14211455
)
1422-
elif orient.lower().startswith("s"):
1456+
1457+
elif orient == "series":
14231458
return into_c((k, com.maybe_box_datetimelike(v)) for k, v in self.items())
1424-
elif orient.lower().startswith("r"):
1459+
1460+
elif orient == "records":
14251461
columns = self.columns.tolist()
14261462
rows = (
14271463
dict(zip(columns, row))
@@ -1431,13 +1467,15 @@ def to_dict(self, orient="dict", into=dict):
14311467
into_c((k, com.maybe_box_datetimelike(v)) for k, v in row.items())
14321468
for row in rows
14331469
]
1434-
elif orient.lower().startswith("i"):
1470+
1471+
elif orient == "index":
14351472
if not self.index.is_unique:
14361473
raise ValueError("DataFrame index must be unique for orient='index'.")
14371474
return into_c(
14381475
(t[0], dict(zip(self.columns, t[1:])))
14391476
for t in self.itertuples(name=None)
14401477
)
1478+
14411479
else:
14421480
raise ValueError(f"orient '{orient}' not understood")
14431481

pandas/tests/frame/methods/test_to_dict.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,17 @@ def test_to_dict_invalid_orient(self):
7070
with pytest.raises(ValueError, match=msg):
7171
df.to_dict(orient="xinvalid")
7272

73+
@pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i"])
74+
def test_to_dict_short_orient_warns(self, orient):
75+
# GH#32515
76+
df = DataFrame({"A": [0, 1]})
77+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
78+
df.to_dict(orient=orient)
79+
7380
@pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict])
7481
def test_to_dict(self, mapping):
82+
# orient= should only take the listed options
83+
# see GH#32515
7584
test_data = {"A": {"1": 1, "2": 2}, "B": {"1": "1", "2": "2", "3": "3"}}
7685

7786
# GH#16122
@@ -81,27 +90,27 @@ def test_to_dict(self, mapping):
8190
for k2, v2 in v.items():
8291
assert v2 == recons_data[k][k2]
8392

84-
recons_data = DataFrame(test_data).to_dict("l", mapping)
93+
recons_data = DataFrame(test_data).to_dict("list", mapping)
8594

8695
for k, v in test_data.items():
8796
for k2, v2 in v.items():
8897
assert v2 == recons_data[k][int(k2) - 1]
8998

90-
recons_data = DataFrame(test_data).to_dict("s", mapping)
99+
recons_data = DataFrame(test_data).to_dict("series", mapping)
91100

92101
for k, v in test_data.items():
93102
for k2, v2 in v.items():
94103
assert v2 == recons_data[k][k2]
95104

96-
recons_data = DataFrame(test_data).to_dict("sp", mapping)
105+
recons_data = DataFrame(test_data).to_dict("split", mapping)
97106
expected_split = {
98107
"columns": ["A", "B"],
99108
"index": ["1", "2", "3"],
100109
"data": [[1.0, "1"], [2.0, "2"], [np.nan, "3"]],
101110
}
102111
tm.assert_dict_equal(recons_data, expected_split)
103112

104-
recons_data = DataFrame(test_data).to_dict("r", mapping)
113+
recons_data = DataFrame(test_data).to_dict("records", mapping)
105114
expected_records = [
106115
{"A": 1.0, "B": "1"},
107116
{"A": 2.0, "B": "2"},
@@ -113,15 +122,15 @@ def test_to_dict(self, mapping):
113122
tm.assert_dict_equal(l, r)
114123

115124
# GH#10844
116-
recons_data = DataFrame(test_data).to_dict("i")
125+
recons_data = DataFrame(test_data).to_dict("index")
117126

118127
for k, v in test_data.items():
119128
for k2, v2 in v.items():
120129
assert v2 == recons_data[k2][k]
121130

122131
df = DataFrame(test_data)
123132
df["duped"] = df[df.columns[0]]
124-
recons_data = df.to_dict("i")
133+
recons_data = df.to_dict("index")
125134
comp_data = test_data.copy()
126135
comp_data["duped"] = comp_data[df.columns[0]]
127136
for k, v in comp_data.items():

0 commit comments

Comments
 (0)