Skip to content

CLN: OrderedDict -> Dict #30497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 2, 2020
7 changes: 2 additions & 5 deletions pandas/core/arrays/sparse/scipy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

Currently only includes to_coo helpers.
"""
from collections import OrderedDict

from pandas.core.indexes.api import Index, MultiIndex
from pandas.core.series import Series

Expand Down Expand Up @@ -46,14 +44,13 @@ def get_indexers(levels):
# labels_to_i[:] = np.arange(labels_to_i.shape[0])

def _get_label_to_i_dict(labels, sort_labels=False):
""" Return OrderedDict of unique labels to number.
""" Return dict of unique labels to number.
Optionally sort by label.
"""
labels = Index(map(tuple, labels)).unique().tolist() # squish
if sort_labels:
labels = sorted(labels)
d = OrderedDict((k, i) for i, k in enumerate(labels))
return d
return {k: i for i, k in enumerate(labels)}

def _get_index_subset_to_coord_dict(index, subset, sort_labels=False):
ilabels = list(zip(*[index._get_level_values(i) for i in subset]))
Expand Down
16 changes: 8 additions & 8 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
These are user facing as the result of the ``df.groupby(...)`` operations,
which here returns a DataFrameGroupBy object.
"""
from collections import OrderedDict, abc, defaultdict, namedtuple
from collections import abc, defaultdict, namedtuple
import copy
from functools import partial
from textwrap import dedent
Expand Down Expand Up @@ -306,7 +306,7 @@ def _aggregate_multiple_funcs(self, arg):

arg = zip(columns, arg)

results = OrderedDict()
results = {}
for name, func in arg:
obj = self

Expand Down Expand Up @@ -443,7 +443,7 @@ def _get_index() -> Index:
return self._reindex_output(result)

def _aggregate_named(self, func, *args, **kwargs):
result = OrderedDict()
result = {}

for name, group in self:
group.name = name
Expand Down Expand Up @@ -1119,7 +1119,7 @@ def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
axis = self.axis
obj = self._obj_with_exclusions

result: OrderedDict = OrderedDict()
result: dict = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the builtins for annotations, rather want to import Dict from typing. Would be extra useful if you can figure out the subtypes that need to be added

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - I ran the tests cases that touch this logic and think I've now got the right subtypes

if axis != obj._info_axis_number:
for name, data in self:
fres = func(data, *args, **kwargs)
Expand All @@ -1136,7 +1136,7 @@ def _aggregate_item_by_item(self, func, *args, **kwargs) -> DataFrame:
# only for axis==0

obj = self._obj_with_exclusions
result: OrderedDict = OrderedDict()
result: dict = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment

cannot_agg = []
for item in obj:
data = obj[item]
Expand Down Expand Up @@ -1874,7 +1874,7 @@ def _normalize_keyword_aggregation(kwargs):
Normalize user-provided "named aggregation" kwargs.

Transforms from the new ``Mapping[str, NamedAgg]`` style kwargs
to the old OrderedDict[str, List[scalar]]].
to the old Dict[str, List[scalar]]].

Parameters
----------
Expand All @@ -1892,11 +1892,11 @@ def _normalize_keyword_aggregation(kwargs):
Examples
--------
>>> _normalize_keyword_aggregation({'output': ('input', 'sum')})
(OrderedDict([('input', ['sum'])]), ('output',), [('input', 'sum')])
({'input': ['sum']}, ('output',), [('input', 'sum')])
"""
# Normalize the aggregation functions as Mapping[column, List[func]],
# process normally, then fixup the names.
# TODO: aggspec type: typing.OrderedDict[str, List[AggScalar]]
# TODO: aggspec type: typing.Dict[str, List[AggScalar]]
# May be hitting https://github.com/python/mypy/issues/5958
# saying it doesn't have an attribute __name__
aggspec = defaultdict(list)
Expand Down
16 changes: 5 additions & 11 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
import datetime
from sys import getsizeof
from typing import List, Optional
Expand Down Expand Up @@ -1639,17 +1638,12 @@ def to_frame(self, index=True, name=None):
else:
idx_names = self.names

# Guarantee resulting column order
# Guarantee resulting column order - PY36+ dict maintains insertion order
result = DataFrame(
OrderedDict(
[
(
(level if lvlname is None else lvlname),
self._get_level_values(level),
)
for lvlname, level in zip(idx_names, range(len(self.levels)))
]
),
{
(level if lvlname is None else lvlname): self._get_level_values(level)
for lvlname, level in zip(idx_names, range(len(self.levels)))
},
copy=False,
)

Expand Down
5 changes: 2 additions & 3 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Module for formatting output data in HTML.
"""

from collections import OrderedDict
from textwrap import dedent
from typing import IO, Any, Dict, Iterable, List, Mapping, Optional, Tuple, Union, cast

Expand Down Expand Up @@ -138,10 +137,10 @@ def _write_cell(
else:
start_tag = "<{kind}>".format(kind=kind)

esc: Union[OrderedDict[str, str], Dict]
esc: Union[Dict[str, str], Dict]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably orthogonal but this is somewhat of a strange annotation - is it not always just Dict[str, str]?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be removed now as it is no longer required to silence Incompatible types in assignment (expression has type "Dict[<nothing>, <nothing>]", variable has type "OrderedDict[str, str]").

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - removed

if self.escape:
# escape & first to prevent double escaping of &
esc = OrderedDict([("&", r"&amp;"), ("<", r"&lt;"), (">", r"&gt;")])
esc = {"&": r"&amp;", "<": r"&lt;", ">": r"&gt;"}
else:
esc = {}

Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
specific classification into the other test modules.
"""
import codecs
from collections import OrderedDict
import csv
from datetime import datetime
from io import BytesIO, StringIO
Expand Down Expand Up @@ -1316,9 +1315,7 @@ def test_float_parser(all_parsers):

def test_scientific_no_exponent(all_parsers):
# see gh-12215
df = DataFrame.from_dict(
OrderedDict([("w", ["2e"]), ("x", ["3E"]), ("y", ["42e"]), ("z", ["632E"])])
)
df = DataFrame.from_dict({"w": ["2e"], "x": ["3E"], "y": ["42e"], "z": ["632E"]})
data = df.to_csv(index=False)
parser = all_parsers

Expand Down
18 changes: 4 additions & 14 deletions pandas/tests/reshape/merge/test_multi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections import OrderedDict

import numpy as np
from numpy.random import randn
import pytest
Expand Down Expand Up @@ -474,17 +472,13 @@ def test_merge_datetime_index(self, klass):
if klass is not None:
on_vector = klass(on_vector)

expected = DataFrame(
OrderedDict([("a", [1, 2, 3]), ("key_1", [2016, 2017, 2018])])
)
expected = DataFrame({"a": [1, 2, 3], "key_1": [2016, 2017, 2018]})

result = df.merge(df, on=["a", on_vector], how="inner")
tm.assert_frame_equal(result, expected)

expected = DataFrame(
OrderedDict(
[("key_0", [2016, 2017, 2018]), ("a_x", [1, 2, 3]), ("a_y", [1, 2, 3])]
)
{"key_0": [2016, 2017, 2018], "a_x": [1, 2, 3], "a_y": [1, 2, 3]}
)

result = df.merge(df, on=[df.index.year], how="inner")
Expand Down Expand Up @@ -788,17 +782,13 @@ def test_merge_datetime_index(self, box):
if box is not None:
on_vector = box(on_vector)

expected = DataFrame(
OrderedDict([("a", [1, 2, 3]), ("key_1", [2016, 2017, 2018])])
)
expected = DataFrame({"a": [1, 2, 3], "key_1": [2016, 2017, 2018]})

result = df.merge(df, on=["a", on_vector], how="inner")
tm.assert_frame_equal(result, expected)

expected = DataFrame(
OrderedDict(
[("key_0", [2016, 2017, 2018]), ("a_x", [1, 2, 3]), ("a_y", [1, 2, 3])]
)
{"key_0": [2016, 2017, 2018], "a_x": [1, 2, 3], "a_y": [1, 2, 3]}
)

result = df.merge(df, on=[df.index.year], how="inner")
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
from datetime import date, datetime, timedelta
from itertools import product

Expand Down Expand Up @@ -1044,7 +1043,7 @@ def test_pivot_columns_lexsorted(self):
assert pivoted.columns.is_monotonic

def test_pivot_complex_aggfunc(self):
f = OrderedDict([("D", ["std"]), ("E", ["sum"])])
f = {"D": ["std"], "E": ["sum"]}
expected = self.data.groupby(["A", "B"]).agg(f).unstack("B")
result = self.data.pivot_table(index="A", columns="B", aggfunc=f)

Expand Down
13 changes: 2 additions & 11 deletions pandas/tests/util/test_validate_args.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections import OrderedDict

import pytest

from pandas.util._validators import validate_args
Expand Down Expand Up @@ -58,11 +56,7 @@ def test_not_all_defaults(i):
r"in the pandas implementation of {func}\(\)".format(arg=bad_arg, func=_fname)
)

compat_args = OrderedDict()
compat_args["foo"] = 2
compat_args["bar"] = -1
compat_args["baz"] = 3

compat_args = {"foo": 2, "bar": -1, "baz": 3}
arg_vals = (1, -1, 3)

with pytest.raises(ValueError, match=msg):
Expand All @@ -73,8 +67,5 @@ def test_validation():
# No exceptions should be raised.
validate_args(_fname, (None,), 2, dict(out=None))

compat_args = OrderedDict()
compat_args["axis"] = 1
compat_args["out"] = None

compat_args = {"axis": 1, "out": None}
validate_args(_fname, (1, None), 2, compat_args)
17 changes: 3 additions & 14 deletions pandas/tests/util/test_validate_args_and_kwargs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections import OrderedDict

import pytest

from pandas.util._validators import validate_args_and_kwargs
Expand Down Expand Up @@ -52,9 +50,7 @@ def test_missing_args_or_kwargs(args, kwargs):
bad_arg = "bar"
min_fname_arg_count = 2

compat_args = OrderedDict()
compat_args["foo"] = -5
compat_args[bad_arg] = 1
compat_args = {"foo": -5, bad_arg: 1}

msg = (
r"the '{arg}' parameter is not supported "
Expand All @@ -68,11 +64,7 @@ def test_missing_args_or_kwargs(args, kwargs):
def test_duplicate_argument():
min_fname_arg_count = 2

compat_args = OrderedDict()
compat_args["foo"] = None
compat_args["bar"] = None
compat_args["baz"] = None

compat_args = {"foo": None, "bar": None, "baz": None}
kwargs = {"foo": None, "bar": None}
args = (None,) # duplicate value for "foo"

Expand All @@ -84,10 +76,7 @@ def test_duplicate_argument():

def test_validation():
# No exceptions should be raised.
compat_args = OrderedDict()
compat_args["foo"] = 1
compat_args["bar"] = None
compat_args["baz"] = -2
compat_args = {"foo": 1, "bar": None, "baz": -2}
kwargs = {"baz": -2}

args = (1, None)
Expand Down
16 changes: 3 additions & 13 deletions pandas/tests/util/test_validate_kwargs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections import OrderedDict

import pytest

from pandas.util._validators import validate_bool_kwarg, validate_kwargs
Expand All @@ -11,9 +9,7 @@ def test_bad_kwarg():
good_arg = "f"
bad_arg = good_arg + "o"

compat_args = OrderedDict()
compat_args[good_arg] = "foo"
compat_args[bad_arg + "o"] = "bar"
compat_args = {good_arg: "foo", bad_arg + "o": "bar"}
kwargs = {good_arg: "foo", bad_arg: "bar"}

msg = fr"{_fname}\(\) got an unexpected keyword argument '{bad_arg}'"
Expand All @@ -30,10 +26,7 @@ def test_not_all_none(i):
r"in the pandas implementation of {func}\(\)".format(arg=bad_arg, func=_fname)
)

compat_args = OrderedDict()
compat_args["foo"] = 1
compat_args["bar"] = "s"
compat_args["baz"] = None
compat_args = {"foo": 1, "bar": "s", "baz": None}

kwarg_keys = ("foo", "bar", "baz")
kwarg_vals = (2, "s", None)
Expand All @@ -46,10 +39,7 @@ def test_not_all_none(i):

def test_validation():
# No exceptions should be raised.
compat_args = OrderedDict()
compat_args["f"] = None
compat_args["b"] = 1
compat_args["ba"] = "s"
compat_args = {"f": None, "b": 1, "ba": "s"}

kwargs = dict(f=None, b=1)
validate_kwargs(_fname, kwargs, compat_args)
Expand Down
17 changes: 7 additions & 10 deletions pandas/util/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ def validate_args(fname, args, max_fname_arg_count, compat_args):
The maximum number of arguments that the function `fname`
can accept, excluding those in `args`. Used for displaying
appropriate error messages. Must be non-negative.
compat_args : Dict
An ordered dictionary of keys and their associated default values.
compat_args : dict
A dictionary of keys and their associated default values.
In order to accommodate buggy behaviour in some versions of `numpy`,
where a signature displayed keyword arguments but then passed those
arguments **positionally** internally when calling downstream
implementations, an ordered dictionary ensures that the original
order of the keyword arguments is enforced. Note that if there is
only one key, a generic dict can be passed in as well.

implementations, a dict ensures that the original
order of the keyword arguments is enforced.
Raises
------
TypeError
Expand Down Expand Up @@ -168,10 +166,9 @@ def validate_args_and_kwargs(fname, args, kwargs, max_fname_arg_count, compat_ar
The minimum number of arguments that the function `fname`
requires, excluding those in `args`. Used for displaying
appropriate error messages. Must be non-negative.
compat_args: OrderedDict
A ordered dictionary of keys that `kwargs` is allowed to
have and their associated default values. Note that if there
is only one key, a generic dict can be passed in as well.
compat_args: dict
A dictionary of keys that `kwargs` is allowed to
have and their associated default values.

Raises
------
Expand Down
5 changes: 2 additions & 3 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""
import argparse
import ast
import collections
import doctest
import functools
import glob
Expand Down Expand Up @@ -422,7 +421,7 @@ def needs_summary(self):

@property
def doc_parameters(self):
parameters = collections.OrderedDict()
parameters = {}
for names, type_, desc in self.doc["Parameters"]:
for name in names.split(", "):
parameters[name] = (type_, "".join(desc))
Expand Down Expand Up @@ -510,7 +509,7 @@ def parameter_desc(self, param):

@property
def see_also(self):
result = collections.OrderedDict()
result = {}
for funcs, desc in self.doc["See Also"]:
for func, _ in funcs:
result[func] = "".join(desc)
Expand Down