From ad4807b5cb414a742161d585767eeba98ae0349b Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Wed, 25 Dec 2019 20:52:33 +0000 Subject: [PATCH 1/2] Remove OrderedDict in base.py --- pandas/core/base.py | 71 +++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 381d45d829e62..948b80fef4032 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -2,7 +2,6 @@ Base and utility classes for pandas objects. """ import builtins -from collections import OrderedDict import textwrap from typing import Dict, FrozenSet, List, Optional @@ -141,39 +140,35 @@ class SelectionMixin: _internal_names = ["_cache", "__setstate__"] _internal_names_set = set(_internal_names) - _builtin_table = OrderedDict( - ((builtins.sum, np.sum), (builtins.max, np.max), (builtins.min, np.min)) - ) - - _cython_table = OrderedDict( - ( - (builtins.sum, "sum"), - (builtins.max, "max"), - (builtins.min, "min"), - (np.all, "all"), - (np.any, "any"), - (np.sum, "sum"), - (np.nansum, "sum"), - (np.mean, "mean"), - (np.nanmean, "mean"), - (np.prod, "prod"), - (np.nanprod, "prod"), - (np.std, "std"), - (np.nanstd, "std"), - (np.var, "var"), - (np.nanvar, "var"), - (np.median, "median"), - (np.nanmedian, "median"), - (np.max, "max"), - (np.nanmax, "max"), - (np.min, "min"), - (np.nanmin, "min"), - (np.cumprod, "cumprod"), - (np.nancumprod, "cumprod"), - (np.cumsum, "cumsum"), - (np.nancumsum, "cumsum"), - ) - ) + _builtin_table = {builtins.sum: np.sum, builtins.max: np.max, builtins.min: np.min} + + _cython_table = { + builtins.sum: "sum", + builtins.max: "max", + builtins.min: "min", + np.all: "all", + np.any: "any", + np.sum: "sum", + np.nansum: "sum", + np.mean: "mean", + np.nanmean: "mean", + np.prod: "prod", + np.nanprod: "prod", + np.std: "std", + np.nanstd: "std", + np.var: "var", + np.nanvar: "var", + np.median: "median", + np.nanmedian: "median", + np.max: "max", + np.nanmax: "max", + np.min: "min", + np.nanmin: "min", + np.cumprod: "cumprod", + np.nancumprod: "cumprod", + np.cumsum: "cumsum", + np.nancumsum: "cumsum", + } @property def _selection_name(self): @@ -328,7 +323,7 @@ def _aggregate(self, arg, *args, **kwargs): # eg. {'A' : ['mean']}, normalize all to # be list-likes if any(is_aggregator(x) for x in arg.values()): - new_arg = OrderedDict() + new_arg = {} for k, v in arg.items(): if not isinstance(v, (tuple, list, dict)): new_arg[k] = [v] @@ -386,16 +381,16 @@ def _agg_2dim(name, how): def _agg(arg, func): """ run the aggregations over the arg with func - return an OrderedDict + return a dict """ - result = OrderedDict() + result = {} for fname, agg_how in arg.items(): result[fname] = func(fname, agg_how) return result # set the final keys keys = list(arg.keys()) - result = OrderedDict() + result = {} if self._selection is not None: From 8ae382567b0543dd55a60994d02b507f020dab15 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Wed, 25 Dec 2019 21:07:54 +0000 Subject: [PATCH 2/2] Remove OrderedDict --- pandas/io/json/_json.py | 4 ++-- pandas/tests/series/test_apply.py | 26 ++++++++++++-------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index c22089b4e1eae..789ff44caf2ee 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -1,4 +1,4 @@ -from collections import OrderedDict, abc +from collections import abc import functools from io import StringIO from itertools import islice @@ -331,7 +331,7 @@ def _write( default_handler, indent, ): - table_obj = OrderedDict((("schema", self.schema), ("data", obj))) + table_obj = {"schema": self.schema, "data": obj} serialized = super()._write( table_obj, orient, diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py index 8956b8b0b2d20..334c6994eb540 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -1,4 +1,4 @@ -from collections import Counter, OrderedDict, defaultdict +from collections import Counter, defaultdict from itertools import chain import numpy as np @@ -297,18 +297,16 @@ def test_replicate_describe(self, string_series): # this also tests a result set that is all scalars expected = string_series.describe() result = string_series.apply( - OrderedDict( - [ - ("count", "count"), - ("mean", "mean"), - ("std", "std"), - ("min", "min"), - ("25%", lambda x: x.quantile(0.25)), - ("50%", "median"), - ("75%", lambda x: x.quantile(0.75)), - ("max", "max"), - ] - ) + { + "count": "count", + "mean": "mean", + "std": "std", + "min": "min", + "25%": lambda x: x.quantile(0.25), + "50%": "median", + "75%": lambda x: x.quantile(0.75), + "max": "max", + } ) tm.assert_series_equal(result, expected) @@ -333,7 +331,7 @@ def test_non_callable_aggregates(self): # test when mixed w/ callable reducers result = s.agg(["size", "count", "mean"]) - expected = Series(OrderedDict([("size", 3.0), ("count", 2.0), ("mean", 1.5)])) + expected = Series({"size": 3.0, "count": 2.0, "mean": 1.5}) tm.assert_series_equal(result[expected.index], expected) @pytest.mark.parametrize(