Skip to content

Commit 8426e56

Browse files
alimcmaster1proost
authored andcommitted
CLN: OrderedDict -> Dict (pandas-dev#29923)
1 parent 1e9461f commit 8426e56

File tree

11 files changed

+54
-89
lines changed

11 files changed

+54
-89
lines changed

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
labeling information
1010
"""
1111
import collections
12-
from collections import OrderedDict, abc
12+
from collections import abc
1313
from io import StringIO
1414
import itertools
1515
import sys
@@ -8170,10 +8170,10 @@ def isin(self, values):
81708170

81718171
def _from_nested_dict(data):
81728172
# TODO: this should be seriously cythonized
8173-
new_data = OrderedDict()
8173+
new_data = {}
81748174
for index, s in data.items():
81758175
for col, v in s.items():
8176-
new_data[col] = new_data.get(col, OrderedDict())
8176+
new_data[col] = new_data.get(col, {})
81778177
new_data[col][index] = v
81788178
return new_data
81798179

pandas/io/excel/_base.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import abc
2-
from collections import OrderedDict
32
from datetime import date, datetime, timedelta
43
from io import BytesIO
54
import os
@@ -429,9 +428,9 @@ def parse(
429428
sheets = [sheet_name]
430429

431430
# handle same-type duplicates.
432-
sheets = list(OrderedDict.fromkeys(sheets).keys())
431+
sheets = list(dict.fromkeys(sheets).keys())
433432

434-
output = OrderedDict()
433+
output = {}
435434

436435
for asheetname in sheets:
437436
if verbose:

pandas/io/stata.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
http://www.statsmodels.org/devel/
1111
"""
1212

13-
from collections import OrderedDict
1413
import datetime
1514
from io import BytesIO
1615
import os
@@ -1676,7 +1675,7 @@ def read(
16761675
else:
16771676
data_formatted.append((col, data[col]))
16781677
if requires_type_conversion:
1679-
data = DataFrame.from_dict(OrderedDict(data_formatted))
1678+
data = DataFrame.from_dict(dict(data_formatted))
16801679
del data_formatted
16811680

16821681
data = self._do_convert_missing(data, convert_missing)
@@ -1715,7 +1714,7 @@ def any_startswith(x: str) -> bool:
17151714
convert = True
17161715
retyped_data.append((col, data[col].astype(dtype)))
17171716
if convert:
1718-
data = DataFrame.from_dict(OrderedDict(retyped_data))
1717+
data = DataFrame.from_dict(dict(retyped_data))
17191718

17201719
if index_col is not None:
17211720
data = data.set_index(data.pop(index_col))
@@ -1845,7 +1844,7 @@ def _do_convert_categoricals(
18451844
cat_converted_data.append((col, cat_data))
18461845
else:
18471846
cat_converted_data.append((col, data[col]))
1848-
data = DataFrame.from_dict(OrderedDict(cat_converted_data))
1847+
data = DataFrame.from_dict(dict(cat_converted_data))
18491848
return data
18501849

18511850
@property
@@ -2194,7 +2193,7 @@ def _prepare_categoricals(self, data):
21942193
data_formatted.append((col, values))
21952194
else:
21962195
data_formatted.append((col, data[col]))
2197-
return DataFrame.from_dict(OrderedDict(data_formatted))
2196+
return DataFrame.from_dict(dict(data_formatted))
21982197

21992198
def _replace_nans(self, data):
22002199
# return data
@@ -2673,7 +2672,7 @@ def __init__(self, df, columns, version=117, byteorder=None):
26732672

26742673
self.df = df
26752674
self.columns = columns
2676-
self._gso_table = OrderedDict((("", (0, 0)),))
2675+
self._gso_table = {"": (0, 0)}
26772676
if byteorder is None:
26782677
byteorder = sys.byteorder
26792678
self._byteorder = _set_endianness(byteorder)
@@ -2703,7 +2702,7 @@ def generate_table(self):
27032702
27042703
Returns
27052704
-------
2706-
gso_table : OrderedDict
2705+
gso_table : dict
27072706
Ordered dictionary using the string found as keys
27082707
and their lookup position (v,o) as values
27092708
gso_df : DataFrame
@@ -2761,7 +2760,7 @@ def generate_blob(self, gso_table):
27612760
27622761
Parameters
27632762
----------
2764-
gso_table : OrderedDict
2763+
gso_table : dict
27652764
Ordered dictionary (str, vo)
27662765
27672766
Returns
@@ -2991,7 +2990,7 @@ def _write_map(self):
29912990
the map with 0s. The second call writes the final map locations when
29922991
all blocks have been written."""
29932992
if self._map is None:
2994-
self._map = OrderedDict(
2993+
self._map = dict(
29952994
(
29962995
("stata_data", 0),
29972996
("map", self._file.tell()),

pandas/tests/groupby/aggregate/test_aggregate.py

+18-35
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
test .agg behavior / note that .apply is tested generally in test_groupby.py
33
"""
4-
from collections import OrderedDict
54
import functools
65

76
import numpy as np
@@ -175,18 +174,14 @@ def test_aggregate_str_func(tsframe, groupbyfunc):
175174
tm.assert_frame_equal(result, expected)
176175

177176
# group frame by function dict
178-
result = grouped.agg(
179-
OrderedDict([["A", "var"], ["B", "std"], ["C", "mean"], ["D", "sem"]])
180-
)
177+
result = grouped.agg({"A": "var", "B": "std", "C": "mean", "D": "sem"})
181178
expected = DataFrame(
182-
OrderedDict(
183-
[
184-
["A", grouped["A"].var()],
185-
["B", grouped["B"].std()],
186-
["C", grouped["C"].mean()],
187-
["D", grouped["D"].sem()],
188-
]
189-
)
179+
{
180+
"A": grouped["A"].var(),
181+
"B": grouped["B"].std(),
182+
"C": grouped["C"].mean(),
183+
"D": grouped["D"].sem(),
184+
}
190185
)
191186
tm.assert_frame_equal(result, expected)
192187

@@ -261,22 +256,20 @@ def test_multiple_functions_tuples_and_non_tuples(df):
261256
def test_more_flexible_frame_multi_function(df):
262257
grouped = df.groupby("A")
263258

264-
exmean = grouped.agg(OrderedDict([["C", np.mean], ["D", np.mean]]))
265-
exstd = grouped.agg(OrderedDict([["C", np.std], ["D", np.std]]))
259+
exmean = grouped.agg({"C": np.mean, "D": np.mean})
260+
exstd = grouped.agg({"C": np.std, "D": np.std})
266261

267262
expected = concat([exmean, exstd], keys=["mean", "std"], axis=1)
268263
expected = expected.swaplevel(0, 1, axis=1).sort_index(level=0, axis=1)
269264

270-
d = OrderedDict([["C", [np.mean, np.std]], ["D", [np.mean, np.std]]])
265+
d = {"C": [np.mean, np.std], "D": [np.mean, np.std]}
271266
result = grouped.aggregate(d)
272267

273268
tm.assert_frame_equal(result, expected)
274269

275270
# be careful
276-
result = grouped.aggregate(OrderedDict([["C", np.mean], ["D", [np.mean, np.std]]]))
277-
expected = grouped.aggregate(
278-
OrderedDict([["C", np.mean], ["D", [np.mean, np.std]]])
279-
)
271+
result = grouped.aggregate({"C": np.mean, "D": [np.mean, np.std]})
272+
expected = grouped.aggregate({"C": np.mean, "D": [np.mean, np.std]})
280273
tm.assert_frame_equal(result, expected)
281274

282275
def foo(x):
@@ -288,13 +281,11 @@ def bar(x):
288281
# this uses column selection & renaming
289282
msg = r"nested renamer is not supported"
290283
with pytest.raises(SpecificationError, match=msg):
291-
d = OrderedDict(
292-
[["C", np.mean], ["D", OrderedDict([["foo", np.mean], ["bar", np.std]])]]
293-
)
284+
d = dict([["C", np.mean], ["D", dict([["foo", np.mean], ["bar", np.std]])]])
294285
grouped.aggregate(d)
295286

296287
# But without renaming, these functions are OK
297-
d = OrderedDict([["C", [np.mean]], ["D", [foo, bar]]])
288+
d = {"C": [np.mean], "D": [foo, bar]}
298289
grouped.aggregate(d)
299290

300291

@@ -303,26 +294,20 @@ def test_multi_function_flexible_mix(df):
303294
grouped = df.groupby("A")
304295

305296
# Expected
306-
d = OrderedDict(
307-
[["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", {"sum": "sum"}]]
308-
)
297+
d = {"C": {"foo": "mean", "bar": "std"}, "D": {"sum": "sum"}}
309298
# this uses column selection & renaming
310299
msg = r"nested renamer is not supported"
311300
with pytest.raises(SpecificationError, match=msg):
312301
grouped.aggregate(d)
313302

314303
# Test 1
315-
d = OrderedDict(
316-
[["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", "sum"]]
317-
)
304+
d = {"C": {"foo": "mean", "bar": "std"}, "D": "sum"}
318305
# this uses column selection & renaming
319306
with pytest.raises(SpecificationError, match=msg):
320307
grouped.aggregate(d)
321308

322309
# Test 2
323-
d = OrderedDict(
324-
[["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", ["sum"]]]
325-
)
310+
d = {"C": {"foo": "mean", "bar": "std"}, "D": "sum"}
326311
# this uses column selection & renaming
327312
with pytest.raises(SpecificationError, match=msg):
328313
grouped.aggregate(d)
@@ -642,9 +627,7 @@ def test_maybe_mangle_lambdas_args(self):
642627
assert func["A"][0](0, 2, b=3) == (0, 2, 3)
643628

644629
def test_maybe_mangle_lambdas_named(self):
645-
func = OrderedDict(
646-
[("C", np.mean), ("D", OrderedDict([("foo", np.mean), ("bar", np.mean)]))]
647-
)
630+
func = {"C": np.mean, "D": {"foo": np.mean, "bar": np.mean}}
648631
result = _maybe_mangle_lambdas(func)
649632
assert result == func
650633

pandas/tests/groupby/aggregate/test_other.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
test all other .agg behavior
33
"""
44

5-
from collections import OrderedDict
65
import datetime as dt
76
from functools import partial
87

@@ -96,8 +95,7 @@ def test_agg_period_index():
9695
index = period_range(start="1999-01", periods=5, freq="M")
9796
s1 = Series(np.random.rand(len(index)), index=index)
9897
s2 = Series(np.random.rand(len(index)), index=index)
99-
series = [("s1", s1), ("s2", s2)]
100-
df = DataFrame.from_dict(OrderedDict(series))
98+
df = DataFrame.from_dict({"s1": s1, "s2": s2})
10199
grouped = df.groupby(df.index.month)
102100
list(grouped)
103101

pandas/tests/groupby/test_categorical.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections import OrderedDict
21
from datetime import datetime
32

43
import numpy as np
@@ -1204,7 +1203,7 @@ def test_seriesgroupby_observed_apply_dict(df_cat, observed, index, data):
12041203
# GH 24880
12051204
expected = Series(data=data, index=index, name="C")
12061205
result = df_cat.groupby(["A", "B"], observed=observed)["C"].apply(
1207-
lambda x: OrderedDict([("min", x.min()), ("max", x.max())])
1206+
lambda x: {"min": x.min(), "max": x.max()}
12081207
)
12091208
tm.assert_series_equal(result, expected)
12101209

pandas/tests/groupby/test_groupby.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections import OrderedDict
21
from datetime import datetime
32
from decimal import Decimal
43
from io import StringIO
@@ -598,7 +597,7 @@ def test_groupby_as_index_agg(df):
598597
expected = grouped.mean()
599598
tm.assert_frame_equal(result, expected)
600599

601-
result2 = grouped.agg(OrderedDict([["C", np.mean], ["D", np.sum]]))
600+
result2 = grouped.agg({"C": np.mean, "D": np.sum})
602601
expected2 = grouped.mean()
603602
expected2["D"] = grouped.sum()["D"]
604603
tm.assert_frame_equal(result2, expected2)
@@ -617,7 +616,7 @@ def test_groupby_as_index_agg(df):
617616
expected = grouped.mean()
618617
tm.assert_frame_equal(result, expected)
619618

620-
result2 = grouped.agg(OrderedDict([["C", np.mean], ["D", np.sum]]))
619+
result2 = grouped.agg({"C": np.mean, "D": np.sum})
621620
expected2 = grouped.mean()
622621
expected2["D"] = grouped.sum()["D"]
623622
tm.assert_frame_equal(result2, expected2)

pandas/tests/indexes/multi/test_constructor.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from collections import OrderedDict
2-
31
import numpy as np
42
import pytest
53

@@ -654,14 +652,12 @@ def test_from_frame_error(non_frame):
654652
def test_from_frame_dtype_fidelity():
655653
# GH 22420
656654
df = pd.DataFrame(
657-
OrderedDict(
658-
[
659-
("dates", pd.date_range("19910905", periods=6, tz="US/Eastern")),
660-
("a", [1, 1, 1, 2, 2, 2]),
661-
("b", pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True)),
662-
("c", ["x", "x", "y", "z", "x", "y"]),
663-
]
664-
)
655+
{
656+
"dates": pd.date_range("19910905", periods=6, tz="US/Eastern"),
657+
"a": [1, 1, 1, 2, 2, 2],
658+
"b": pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True),
659+
"c": ["x", "x", "y", "z", "x", "y"],
660+
}
665661
)
666662
original_dtypes = df.dtypes.to_dict()
667663

pandas/tests/indexes/multi/test_conversion.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from collections import OrderedDict
2-
31
import numpy as np
42
import pytest
53

@@ -107,14 +105,12 @@ def test_to_frame_dtype_fidelity():
107105
original_dtypes = {name: mi.levels[i].dtype for i, name in enumerate(mi.names)}
108106

109107
expected_df = pd.DataFrame(
110-
OrderedDict(
111-
[
112-
("dates", pd.date_range("19910905", periods=6, tz="US/Eastern")),
113-
("a", [1, 1, 1, 2, 2, 2]),
114-
("b", pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True)),
115-
("c", ["x", "x", "y", "z", "x", "y"]),
116-
]
117-
)
108+
{
109+
"dates": pd.date_range("19910905", periods=6, tz="US/Eastern"),
110+
"a": [1, 1, 1, 2, 2, 2],
111+
"b": pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True),
112+
"c": ["x", "x", "y", "z", "x", "y"],
113+
}
118114
)
119115
df = mi.to_frame(index=False)
120116
df_dtypes = df.dtypes.to_dict()

pandas/tests/indexes/test_setops.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
The tests in this package are to ensure the proper resultant dtypes of
33
set operations.
44
"""
5-
from collections import OrderedDict
65
import itertools as it
76

87
import numpy as np
@@ -16,14 +15,12 @@
1615
from pandas.tests.indexes.conftest import indices_dict
1716
import pandas.util.testing as tm
1817

19-
COMPATIBLE_INCONSISTENT_PAIRS = OrderedDict(
20-
[
21-
((Int64Index, RangeIndex), (tm.makeIntIndex, tm.makeRangeIndex)),
22-
((Float64Index, Int64Index), (tm.makeFloatIndex, tm.makeIntIndex)),
23-
((Float64Index, RangeIndex), (tm.makeFloatIndex, tm.makeIntIndex)),
24-
((Float64Index, UInt64Index), (tm.makeFloatIndex, tm.makeUIntIndex)),
25-
]
26-
)
18+
COMPATIBLE_INCONSISTENT_PAIRS = {
19+
(Int64Index, RangeIndex): (tm.makeIntIndex, tm.makeRangeIndex),
20+
(Float64Index, Int64Index): (tm.makeFloatIndex, tm.makeIntIndex),
21+
(Float64Index, RangeIndex): (tm.makeFloatIndex, tm.makeIntIndex),
22+
(Float64Index, UInt64Index): (tm.makeFloatIndex, tm.makeUIntIndex),
23+
}
2724

2825

2926
@pytest.fixture(params=it.combinations(indices_dict, 2), ids="-".join)

pandas/tests/io/test_stata.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections import OrderedDict
21
import datetime as dt
32
from datetime import datetime
43
import gzip
@@ -1029,7 +1028,7 @@ def test_categorical_order(self, file):
10291028
cols.append((col, pd.Categorical.from_codes(codes, labels)))
10301029
else:
10311030
cols.append((col, pd.Series(labels, dtype=np.float32)))
1032-
expected = DataFrame.from_dict(OrderedDict(cols))
1031+
expected = DataFrame.from_dict(dict(cols))
10331032

10341033
# Read with and with out categoricals, ensure order is identical
10351034
file = getattr(self, file)

0 commit comments

Comments
 (0)