Skip to content

Commit 7075467

Browse files
committed
added deprecation warning and rolled back to accepting short versions of orient
1 parent e069a60 commit 7075467

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

pandas/core/frame.py

+23
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,29 @@ def to_dict(self, orient="dict", into=dict):
14001400
# GH16122
14011401
into_c = com.standardize_mapping(into)
14021402
orient = orient.lower()
1403+
# GH32515
1404+
if orient not in {"dict", "list", "series", "split", "records", "index"}:
1405+
warnings.warn(
1406+
"Using short name for 'orient' is deprecated. Only the "
1407+
"options: ('dict', list, 'series', 'split', 'records', 'index') "
1408+
"will be used in a future version. Use one of the above "
1409+
"to silence this warning.",
1410+
DeprecationWarning,
1411+
stacklevel=2)
1412+
1413+
if orient.startswith("d"):
1414+
orient = "dict"
1415+
elif orient.startswith("l"):
1416+
orient = "list"
1417+
elif orient.startswith("sp"):
1418+
orient = "split"
1419+
elif orient.startswith("s"):
1420+
orient = "series"
1421+
elif orient.startswith("r"):
1422+
orient = "records"
1423+
elif orient.startswith("i"):
1424+
orient = "index"
1425+
14031426
if orient == "dict":
14041427
return into_c((k, v.to_dict(into)) for k, v in self.items())
14051428

pandas/tests/frame/methods/test_to_dict.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,22 @@ def test_to_dict_index_not_unique_with_index_orient(self):
6464
with pytest.raises(ValueError, match=msg):
6565
df.to_dict(orient="index")
6666

67-
@pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i", "xinvalid"])
67+
@pytest.mark.parametrize("orient", ["xinvalid"])
6868
def test_to_dict_invalid_orient(self, orient):
69-
# to_dict(orient='d') should fail, as should only take the listed options
70-
# see GH#32515
71-
7269
df = DataFrame({"A": [0, 1]})
7370
msg = f"orient '{orient}' not understood"
7471
with pytest.raises(ValueError, match=msg):
7572
df.to_dict(orient=orient)
7673

74+
@pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i"])
75+
def test_to_dict_short_orient(self, orient):
76+
# to_dict(orient='d') or any other short option should raise DeprecationWarning
77+
# see GH#32515
78+
df = DataFrame({"A": [0, 1]})
79+
with pytest.warns(DeprecationWarning):
80+
df.to_dict(orient=orient)
81+
82+
7783
@pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict])
7884
def test_to_dict(self, mapping):
7985
# orient= should only take the listed options

0 commit comments

Comments
 (0)