From a9096e429a8d229fe3863a7c3d03afe10ed7d55f Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sat, 9 Nov 2019 17:25:19 +0000 Subject: [PATCH 1/7] OrderedDict -> Dict --- pandas/io/excel/_base.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index d0ab6dd37596c..d2f803b6a6792 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -1,5 +1,4 @@ import abc -from collections import OrderedDict from datetime import date, datetime, timedelta from io import BytesIO import os @@ -435,9 +434,9 @@ def parse( sheets = [sheet_name] # handle same-type duplicates. - sheets = list(OrderedDict.fromkeys(sheets).keys()) + sheets = list(dict.fromkeys(sheets).keys()) - output = OrderedDict() + output = {} for asheetname in sheets: if verbose: From 2d70348d62e2d7b285ae49662fa06001e69f9ab9 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sat, 9 Nov 2019 17:34:31 +0000 Subject: [PATCH 2/7] Run black --- pandas/core/common.py | 4 ++-- pandas/core/frame.py | 14 +++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 565f5076fdddb..49ed9dde805af 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -5,7 +5,7 @@ """ import collections -from collections import OrderedDict, abc +from collections import abc from datetime import datetime, timedelta from functools import partial import inspect @@ -218,7 +218,7 @@ def try_sort(iterable): def dict_keys_to_ordered_list(mapping): # when pandas drops support for Python < 3.6, this function # can be replaced by a simple list(mapping.keys()) - if PY36 or isinstance(mapping, OrderedDict): + if PY36 or isinstance(mapping, dict): keys = list(mapping.keys()) else: keys = try_sort(mapping) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7e3c2200dbabc..18c55a2652a02 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -9,7 +9,7 @@ labeling information """ import collections -from collections import OrderedDict, abc +from collections import abc from io import StringIO import itertools import sys @@ -1880,8 +1880,6 @@ def from_items(cls, items, columns=None, orient="columns"): `from_items` is deprecated and will be removed in a future version. Use :meth:`DataFrame.from_dict(dict(items)) ` instead. - :meth:`DataFrame.from_dict(OrderedDict(items)) ` - may be used to preserve the key order. Convert (key, value) pairs to DataFrame. The keys will be the axis index (usually the columns, but depends on the specified @@ -1906,9 +1904,7 @@ def from_items(cls, items, columns=None, orient="columns"): warnings.warn( "from_items is deprecated. Please use " - "DataFrame.from_dict(dict(items), ...) instead. " - "DataFrame.from_dict(OrderedDict(items)) may be used to " - "preserve the key order.", + "DataFrame.from_dict(dict(items), ...) instead. ", FutureWarning, stacklevel=2, ) @@ -3551,7 +3547,7 @@ def assign(self, **kwargs): data[k] = com.apply_if_callable(v, data) else: # <= 3.5: do all calculations first... - results = OrderedDict() + results = {} for k, v in kwargs.items(): results[k] = com.apply_if_callable(v, data) @@ -8340,10 +8336,10 @@ def isin(self, values): def _from_nested_dict(data): # TODO: this should be seriously cythonized - new_data = OrderedDict() + new_data = {} for index, s in data.items(): for col, v in s.items(): - new_data[col] = new_data.get(col, OrderedDict()) + new_data[col] = new_data.get(col, {}) new_data[col][index] = v return new_data From 1d2a061e26179a8ecc4241ab4dd090403cfe21c9 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Thu, 28 Nov 2019 23:36:29 +0000 Subject: [PATCH 3/7] OrderedDict -> Dict --- pandas/io/stata.py | 17 ++++++++--------- pandas/tests/groupby/test_categorical.py | 3 +-- pandas/tests/groupby/test_groupby.py | 5 ++--- pandas/tests/io/test_stata.py | 3 +-- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index bd5e215730397..afa19441dce1b 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -10,7 +10,6 @@ http://www.statsmodels.org/devel/ """ -from collections import OrderedDict import datetime from io import BytesIO import os @@ -1677,7 +1676,7 @@ def read( else: data_formatted.append((col, data[col])) if requires_type_conversion: - data = DataFrame.from_dict(OrderedDict(data_formatted)) + data = DataFrame.from_dict(dict(data_formatted)) del data_formatted data = self._do_convert_missing(data, convert_missing) @@ -1716,7 +1715,7 @@ def any_startswith(x: str) -> bool: convert = True retyped_data.append((col, data[col].astype(dtype))) if convert: - data = DataFrame.from_dict(OrderedDict(retyped_data)) + data = DataFrame.from_dict(dict(retyped_data)) if index_col is not None: data = data.set_index(data.pop(index_col)) @@ -1846,7 +1845,7 @@ def _do_convert_categoricals( cat_converted_data.append((col, cat_data)) else: cat_converted_data.append((col, data[col])) - data = DataFrame.from_dict(OrderedDict(cat_converted_data)) + data = DataFrame.from_dict(dict(cat_converted_data)) return data @property @@ -2195,7 +2194,7 @@ def _prepare_categoricals(self, data): data_formatted.append((col, values)) else: data_formatted.append((col, data[col])) - return DataFrame.from_dict(OrderedDict(data_formatted)) + return DataFrame.from_dict(dict(data_formatted)) def _replace_nans(self, data): # return data @@ -2674,7 +2673,7 @@ def __init__(self, df, columns, version=117, byteorder=None): self.df = df self.columns = columns - self._gso_table = OrderedDict((("", (0, 0)),)) + self._gso_table = dict((("", (0, 0)),)) if byteorder is None: byteorder = sys.byteorder self._byteorder = _set_endianness(byteorder) @@ -2704,7 +2703,7 @@ def generate_table(self): Returns ------- - gso_table : OrderedDict + gso_table : dict Ordered dictionary using the string found as keys and their lookup position (v,o) as values gso_df : DataFrame @@ -2762,7 +2761,7 @@ def generate_blob(self, gso_table): Parameters ---------- - gso_table : OrderedDict + gso_table : dict Ordered dictionary (str, vo) Returns @@ -2992,7 +2991,7 @@ def _write_map(self): the map with 0s. The second call writes the final map locations when all blocks have been written.""" if self._map is None: - self._map = OrderedDict( + self._map = dict( ( ("stata_data", 0), ("map", self._file.tell()), diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 5f78e4860f1e9..7f5b78175959b 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1,4 +1,3 @@ -from collections import OrderedDict from datetime import datetime import numpy as np @@ -1204,7 +1203,7 @@ def test_seriesgroupby_observed_apply_dict(df_cat, observed, index, data): # GH 24880 expected = Series(data=data, index=index, name="C") result = df_cat.groupby(["A", "B"], observed=observed)["C"].apply( - lambda x: OrderedDict([("min", x.min()), ("max", x.max())]) + lambda x: dict([("min", x.min()), ("max", x.max())]) ) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index a6b9b0e35f865..e0e905cc7cf0a 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -1,4 +1,3 @@ -from collections import OrderedDict from datetime import datetime from decimal import Decimal from io import StringIO @@ -598,7 +597,7 @@ def test_groupby_as_index_agg(df): expected = grouped.mean() tm.assert_frame_equal(result, expected) - result2 = grouped.agg(OrderedDict([["C", np.mean], ["D", np.sum]])) + result2 = grouped.agg(dict([["C", np.mean], ["D", np.sum]])) expected2 = grouped.mean() expected2["D"] = grouped.sum()["D"] tm.assert_frame_equal(result2, expected2) @@ -617,7 +616,7 @@ def test_groupby_as_index_agg(df): expected = grouped.mean() tm.assert_frame_equal(result, expected) - result2 = grouped.agg(OrderedDict([["C", np.mean], ["D", np.sum]])) + result2 = grouped.agg(dict([["C", np.mean], ["D", np.sum]])) expected2 = grouped.mean() expected2["D"] = grouped.sum()["D"] tm.assert_frame_equal(result2, expected2) diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index 2cc80a6e5565d..4203d0b0241ff 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -1,4 +1,3 @@ -from collections import OrderedDict import datetime as dt from datetime import datetime import gzip @@ -1029,7 +1028,7 @@ def test_categorical_order(self, file): cols.append((col, pd.Categorical.from_codes(codes, labels))) else: cols.append((col, pd.Series(labels, dtype=np.float32))) - expected = DataFrame.from_dict(OrderedDict(cols)) + expected = DataFrame.from_dict(dict(cols)) # Read with and with out categoricals, ensure order is identical file = getattr(self, file) From 813dedc91597b2a39deed81d8b38420d2df60c30 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Thu, 28 Nov 2019 23:42:13 +0000 Subject: [PATCH 4/7] OrderedDict -> Dict --- .../tests/groupby/aggregate/test_aggregate.py | 37 +++++++++---------- pandas/tests/groupby/aggregate/test_other.py | 3 +- .../tests/indexes/multi/test_constructor.py | 4 +- pandas/tests/indexes/multi/test_conversion.py | 3 +- pandas/tests/indexes/test_setops.py | 3 +- 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index e4de2147586f5..392531fa817db 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1,7 +1,6 @@ """ test .agg behavior / note that .apply is tested generally in test_groupby.py """ -from collections import OrderedDict import functools import numpy as np @@ -176,10 +175,10 @@ def test_aggregate_str_func(tsframe, groupbyfunc): # group frame by function dict result = grouped.agg( - OrderedDict([["A", "var"], ["B", "std"], ["C", "mean"], ["D", "sem"]]) + dict([["A", "var"], ["B", "std"], ["C", "mean"], ["D", "sem"]]) ) expected = DataFrame( - OrderedDict( + dict( [ ["A", grouped["A"].var()], ["B", grouped["B"].std()], @@ -261,21 +260,21 @@ def test_multiple_functions_tuples_and_non_tuples(df): def test_more_flexible_frame_multi_function(df): grouped = df.groupby("A") - exmean = grouped.agg(OrderedDict([["C", np.mean], ["D", np.mean]])) - exstd = grouped.agg(OrderedDict([["C", np.std], ["D", np.std]])) + exmean = grouped.agg(dict([["C", np.mean], ["D", np.mean]])) + exstd = grouped.agg(dict([["C", np.std], ["D", np.std]])) expected = concat([exmean, exstd], keys=["mean", "std"], axis=1) expected = expected.swaplevel(0, 1, axis=1).sort_index(level=0, axis=1) - d = OrderedDict([["C", [np.mean, np.std]], ["D", [np.mean, np.std]]]) + d = dict([["C", [np.mean, np.std]], ["D", [np.mean, np.std]]]) result = grouped.aggregate(d) tm.assert_frame_equal(result, expected) # be careful - result = grouped.aggregate(OrderedDict([["C", np.mean], ["D", [np.mean, np.std]]])) + result = grouped.aggregate(dict([["C", np.mean], ["D", [np.mean, np.std]]])) expected = grouped.aggregate( - OrderedDict([["C", np.mean], ["D", [np.mean, np.std]]]) + dict([["C", np.mean], ["D", [np.mean, np.std]]]) ) tm.assert_frame_equal(result, expected) @@ -288,13 +287,13 @@ def bar(x): # this uses column selection & renaming msg = r"nested renamer is not supported" with pytest.raises(SpecificationError, match=msg): - d = OrderedDict( - [["C", np.mean], ["D", OrderedDict([["foo", np.mean], ["bar", np.std]])]] + d = dict( + [["C", np.mean], ["D", dict([["foo", np.mean], ["bar", np.std]])]] ) grouped.aggregate(d) # But without renaming, these functions are OK - d = OrderedDict([["C", [np.mean]], ["D", [foo, bar]]]) + d = dict([["C", [np.mean]], ["D", [foo, bar]]]) grouped.aggregate(d) @@ -303,8 +302,8 @@ def test_multi_function_flexible_mix(df): grouped = df.groupby("A") # Expected - d = OrderedDict( - [["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", {"sum": "sum"}]] + d = dict( + [["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", {"sum": "sum"}]] ) # this uses column selection & renaming msg = r"nested renamer is not supported" @@ -312,16 +311,16 @@ def test_multi_function_flexible_mix(df): grouped.aggregate(d) # Test 1 - d = OrderedDict( - [["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", "sum"]] + d = dict( + [["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", "sum"]] ) # this uses column selection & renaming with pytest.raises(SpecificationError, match=msg): grouped.aggregate(d) # Test 2 - d = OrderedDict( - [["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", ["sum"]]] + d = dict( + [["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", ["sum"]]] ) # this uses column selection & renaming with pytest.raises(SpecificationError, match=msg): @@ -642,8 +641,8 @@ def test_maybe_mangle_lambdas_args(self): assert func["A"][0](0, 2, b=3) == (0, 2, 3) def test_maybe_mangle_lambdas_named(self): - func = OrderedDict( - [("C", np.mean), ("D", OrderedDict([("foo", np.mean), ("bar", np.mean)]))] + func = dict( + [("C", np.mean), ("D", dict([("foo", np.mean), ("bar", np.mean)]))] ) result = _maybe_mangle_lambdas(func) assert result == func diff --git a/pandas/tests/groupby/aggregate/test_other.py b/pandas/tests/groupby/aggregate/test_other.py index f14384928b979..ccf031dfc0c51 100644 --- a/pandas/tests/groupby/aggregate/test_other.py +++ b/pandas/tests/groupby/aggregate/test_other.py @@ -2,7 +2,6 @@ test all other .agg behavior """ -from collections import OrderedDict import datetime as dt from functools import partial @@ -97,7 +96,7 @@ def test_agg_period_index(): s1 = Series(np.random.rand(len(index)), index=index) s2 = Series(np.random.rand(len(index)), index=index) series = [("s1", s1), ("s2", s2)] - df = DataFrame.from_dict(OrderedDict(series)) + df = DataFrame.from_dict(dict(series)) grouped = df.groupby(df.index.month) list(grouped) diff --git a/pandas/tests/indexes/multi/test_constructor.py b/pandas/tests/indexes/multi/test_constructor.py index c0ec889d170d6..dd30ebfd9d451 100644 --- a/pandas/tests/indexes/multi/test_constructor.py +++ b/pandas/tests/indexes/multi/test_constructor.py @@ -1,5 +1,3 @@ -from collections import OrderedDict - import numpy as np import pytest @@ -654,7 +652,7 @@ def test_from_frame_error(non_frame): def test_from_frame_dtype_fidelity(): # GH 22420 df = pd.DataFrame( - OrderedDict( + dict( [ ("dates", pd.date_range("19910905", periods=6, tz="US/Eastern")), ("a", [1, 1, 1, 2, 2, 2]), diff --git a/pandas/tests/indexes/multi/test_conversion.py b/pandas/tests/indexes/multi/test_conversion.py index a0b17ae8924b7..fbf9ef4e224e8 100644 --- a/pandas/tests/indexes/multi/test_conversion.py +++ b/pandas/tests/indexes/multi/test_conversion.py @@ -1,4 +1,3 @@ -from collections import OrderedDict import numpy as np import pytest @@ -107,7 +106,7 @@ def test_to_frame_dtype_fidelity(): original_dtypes = {name: mi.levels[i].dtype for i, name in enumerate(mi.names)} expected_df = pd.DataFrame( - OrderedDict( + dict( [ ("dates", pd.date_range("19910905", periods=6, tz="US/Eastern")), ("a", [1, 1, 1, 2, 2, 2]), diff --git a/pandas/tests/indexes/test_setops.py b/pandas/tests/indexes/test_setops.py index d5b23653e8a72..be61d8b5dda27 100644 --- a/pandas/tests/indexes/test_setops.py +++ b/pandas/tests/indexes/test_setops.py @@ -2,7 +2,6 @@ The tests in this package are to ensure the proper resultant dtypes of set operations. """ -from collections import OrderedDict import itertools as it import numpy as np @@ -16,7 +15,7 @@ from pandas.tests.indexes.conftest import indices_dict import pandas.util.testing as tm -COMPATIBLE_INCONSISTENT_PAIRS = OrderedDict( +COMPATIBLE_INCONSISTENT_PAIRS = dict( [ ((Int64Index, RangeIndex), (tm.makeIntIndex, tm.makeRangeIndex)), ((Float64Index, Int64Index), (tm.makeFloatIndex, tm.makeIntIndex)), From 6271531486d3f7044aad8ffd5fdf456de06d12e3 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Fri, 29 Nov 2019 00:29:25 +0000 Subject: [PATCH 5/7] run black --- .../tests/groupby/aggregate/test_aggregate.py | 24 +++++-------------- pandas/tests/indexes/multi/test_conversion.py | 1 - 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 392531fa817db..f9b05d9110fea 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -273,9 +273,7 @@ def test_more_flexible_frame_multi_function(df): # be careful result = grouped.aggregate(dict([["C", np.mean], ["D", [np.mean, np.std]]])) - expected = grouped.aggregate( - dict([["C", np.mean], ["D", [np.mean, np.std]]]) - ) + expected = grouped.aggregate(dict([["C", np.mean], ["D", [np.mean, np.std]]])) tm.assert_frame_equal(result, expected) def foo(x): @@ -287,9 +285,7 @@ def bar(x): # this uses column selection & renaming msg = r"nested renamer is not supported" with pytest.raises(SpecificationError, match=msg): - d = dict( - [["C", np.mean], ["D", dict([["foo", np.mean], ["bar", np.std]])]] - ) + d = dict([["C", np.mean], ["D", dict([["foo", np.mean], ["bar", np.std]])]]) grouped.aggregate(d) # But without renaming, these functions are OK @@ -302,26 +298,20 @@ def test_multi_function_flexible_mix(df): grouped = df.groupby("A") # Expected - d = dict( - [["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", {"sum": "sum"}]] - ) + d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", {"sum": "sum"}]]) # this uses column selection & renaming msg = r"nested renamer is not supported" with pytest.raises(SpecificationError, match=msg): grouped.aggregate(d) # Test 1 - d = dict( - [["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", "sum"]] - ) + d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", "sum"]]) # this uses column selection & renaming with pytest.raises(SpecificationError, match=msg): grouped.aggregate(d) # Test 2 - d = dict( - [["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", ["sum"]]] - ) + d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", ["sum"]]]) # this uses column selection & renaming with pytest.raises(SpecificationError, match=msg): grouped.aggregate(d) @@ -641,9 +631,7 @@ def test_maybe_mangle_lambdas_args(self): assert func["A"][0](0, 2, b=3) == (0, 2, 3) def test_maybe_mangle_lambdas_named(self): - func = dict( - [("C", np.mean), ("D", dict([("foo", np.mean), ("bar", np.mean)]))] - ) + func = dict([("C", np.mean), ("D", dict([("foo", np.mean), ("bar", np.mean)]))]) result = _maybe_mangle_lambdas(func) assert result == func diff --git a/pandas/tests/indexes/multi/test_conversion.py b/pandas/tests/indexes/multi/test_conversion.py index fbf9ef4e224e8..d5dfa73bc64fe 100644 --- a/pandas/tests/indexes/multi/test_conversion.py +++ b/pandas/tests/indexes/multi/test_conversion.py @@ -1,4 +1,3 @@ - import numpy as np import pytest From a1bb310dc0a1d7bb6cbfb510a6f8e5334f1f921a Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sat, 30 Nov 2019 20:34:04 +0000 Subject: [PATCH 6/7] Use literals --- pandas/io/stata.py | 2 +- .../tests/groupby/aggregate/test_aggregate.py | 36 +++++++++---------- pandas/tests/groupby/aggregate/test_other.py | 3 +- pandas/tests/groupby/test_categorical.py | 2 +- pandas/tests/groupby/test_groupby.py | 4 +-- .../tests/indexes/multi/test_constructor.py | 14 ++++---- pandas/tests/indexes/multi/test_conversion.py | 14 ++++---- pandas/tests/indexes/test_setops.py | 14 ++++---- 8 files changed, 39 insertions(+), 50 deletions(-) diff --git a/pandas/io/stata.py b/pandas/io/stata.py index afa19441dce1b..1f90bb12e11a3 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -2673,7 +2673,7 @@ def __init__(self, df, columns, version=117, byteorder=None): self.df = df self.columns = columns - self._gso_table = dict((("", (0, 0)),)) + self._gso_table = {"": (0, 0)} if byteorder is None: byteorder = sys.byteorder self._byteorder = _set_endianness(byteorder) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index f9b05d9110fea..53dda2693dcf6 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -174,18 +174,14 @@ def test_aggregate_str_func(tsframe, groupbyfunc): tm.assert_frame_equal(result, expected) # group frame by function dict - result = grouped.agg( - dict([["A", "var"], ["B", "std"], ["C", "mean"], ["D", "sem"]]) - ) + result = grouped.agg({"A": "var", "B": "std", "C": "mean", "D": "sem"}) expected = DataFrame( - dict( - [ - ["A", grouped["A"].var()], - ["B", grouped["B"].std()], - ["C", grouped["C"].mean()], - ["D", grouped["D"].sem()], - ] - ) + { + "A": grouped["A"].var(), + "B": grouped["B"].std(), + "C": grouped["C"].mean(), + "D": grouped["D"].sem(), + } ) tm.assert_frame_equal(result, expected) @@ -260,8 +256,8 @@ def test_multiple_functions_tuples_and_non_tuples(df): def test_more_flexible_frame_multi_function(df): grouped = df.groupby("A") - exmean = grouped.agg(dict([["C", np.mean], ["D", np.mean]])) - exstd = grouped.agg(dict([["C", np.std], ["D", np.std]])) + exmean = grouped.agg({"C": np.mean, "D": np.mean}) + exstd = grouped.agg({"C", np.std, "D", np.std}) expected = concat([exmean, exstd], keys=["mean", "std"], axis=1) expected = expected.swaplevel(0, 1, axis=1).sort_index(level=0, axis=1) @@ -272,8 +268,8 @@ def test_more_flexible_frame_multi_function(df): tm.assert_frame_equal(result, expected) # be careful - result = grouped.aggregate(dict([["C", np.mean], ["D", [np.mean, np.std]]])) - expected = grouped.aggregate(dict([["C", np.mean], ["D", [np.mean, np.std]]])) + result = grouped.aggregate({"C": np.mean, "D": [np.mean, np.std]}) + expected = grouped.aggregate({"C": np.mean, "D": [np.mean, np.std]}) tm.assert_frame_equal(result, expected) def foo(x): @@ -289,7 +285,7 @@ def bar(x): grouped.aggregate(d) # But without renaming, these functions are OK - d = dict([["C", [np.mean]], ["D", [foo, bar]]]) + d = {"C": [np.mean], "D": [foo, bar]} grouped.aggregate(d) @@ -298,20 +294,20 @@ def test_multi_function_flexible_mix(df): grouped = df.groupby("A") # Expected - d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", {"sum": "sum"}]]) + d = {"C": {"foo": "mean", "bar": "std"}, "D": {"sum": "sum"}} # this uses column selection & renaming msg = r"nested renamer is not supported" with pytest.raises(SpecificationError, match=msg): grouped.aggregate(d) # Test 1 - d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", "sum"]]) + d = {"C": {"foo": "mean", "bar": "std"}, "D": "sum"} # this uses column selection & renaming with pytest.raises(SpecificationError, match=msg): grouped.aggregate(d) # Test 2 - d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", ["sum"]]]) + d = {"C": {"foo": "mean", "bar": "std"}, "D": "sum"} # this uses column selection & renaming with pytest.raises(SpecificationError, match=msg): grouped.aggregate(d) @@ -631,7 +627,7 @@ def test_maybe_mangle_lambdas_args(self): assert func["A"][0](0, 2, b=3) == (0, 2, 3) def test_maybe_mangle_lambdas_named(self): - func = dict([("C", np.mean), ("D", dict([("foo", np.mean), ("bar", np.mean)]))]) + func = {"C": np.mean, "D": {"foo": np.mean, "bar": np.mean}} result = _maybe_mangle_lambdas(func) assert result == func diff --git a/pandas/tests/groupby/aggregate/test_other.py b/pandas/tests/groupby/aggregate/test_other.py index ccf031dfc0c51..765bc3bab5d4a 100644 --- a/pandas/tests/groupby/aggregate/test_other.py +++ b/pandas/tests/groupby/aggregate/test_other.py @@ -95,8 +95,7 @@ def test_agg_period_index(): index = period_range(start="1999-01", periods=5, freq="M") s1 = Series(np.random.rand(len(index)), index=index) s2 = Series(np.random.rand(len(index)), index=index) - series = [("s1", s1), ("s2", s2)] - df = DataFrame.from_dict(dict(series)) + df = DataFrame.from_dict({"s1": s1, "s2": s2}) grouped = df.groupby(df.index.month) list(grouped) diff --git a/pandas/tests/groupby/test_categorical.py b/pandas/tests/groupby/test_categorical.py index 7f5b78175959b..89ffcd9ee313e 100644 --- a/pandas/tests/groupby/test_categorical.py +++ b/pandas/tests/groupby/test_categorical.py @@ -1203,7 +1203,7 @@ def test_seriesgroupby_observed_apply_dict(df_cat, observed, index, data): # GH 24880 expected = Series(data=data, index=index, name="C") result = df_cat.groupby(["A", "B"], observed=observed)["C"].apply( - lambda x: dict([("min", x.min()), ("max", x.max())]) + lambda x: {"min": x.min(), "max": x.max()} ) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index e0e905cc7cf0a..3a16642641fca 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -597,7 +597,7 @@ def test_groupby_as_index_agg(df): expected = grouped.mean() tm.assert_frame_equal(result, expected) - result2 = grouped.agg(dict([["C", np.mean], ["D", np.sum]])) + result2 = grouped.agg({"C": np.mean, "D": np.sum}) expected2 = grouped.mean() expected2["D"] = grouped.sum()["D"] tm.assert_frame_equal(result2, expected2) @@ -616,7 +616,7 @@ def test_groupby_as_index_agg(df): expected = grouped.mean() tm.assert_frame_equal(result, expected) - result2 = grouped.agg(dict([["C", np.mean], ["D", np.sum]])) + result2 = grouped.agg({"C": np.mean, "D": np.sum}) expected2 = grouped.mean() expected2["D"] = grouped.sum()["D"] tm.assert_frame_equal(result2, expected2) diff --git a/pandas/tests/indexes/multi/test_constructor.py b/pandas/tests/indexes/multi/test_constructor.py index dd30ebfd9d451..90e993a807bd2 100644 --- a/pandas/tests/indexes/multi/test_constructor.py +++ b/pandas/tests/indexes/multi/test_constructor.py @@ -652,14 +652,12 @@ def test_from_frame_error(non_frame): def test_from_frame_dtype_fidelity(): # GH 22420 df = pd.DataFrame( - dict( - [ - ("dates", pd.date_range("19910905", periods=6, tz="US/Eastern")), - ("a", [1, 1, 1, 2, 2, 2]), - ("b", pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True)), - ("c", ["x", "x", "y", "z", "x", "y"]), - ] - ) + { + "dates": pd.date_range("19910905", periods=6, tz="US/Eastern"), + "a": [1, 1, 1, 2, 2, 2], + "b": pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True), + "c": ["x", "x", "y", "z", "x", "y"], + } ) original_dtypes = df.dtypes.to_dict() diff --git a/pandas/tests/indexes/multi/test_conversion.py b/pandas/tests/indexes/multi/test_conversion.py index d5dfa73bc64fe..fab4f72dc153b 100644 --- a/pandas/tests/indexes/multi/test_conversion.py +++ b/pandas/tests/indexes/multi/test_conversion.py @@ -105,14 +105,12 @@ def test_to_frame_dtype_fidelity(): original_dtypes = {name: mi.levels[i].dtype for i, name in enumerate(mi.names)} expected_df = pd.DataFrame( - dict( - [ - ("dates", pd.date_range("19910905", periods=6, tz="US/Eastern")), - ("a", [1, 1, 1, 2, 2, 2]), - ("b", pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True)), - ("c", ["x", "x", "y", "z", "x", "y"]), - ] - ) + { + "dates": pd.date_range("19910905", periods=6, tz="US/Eastern"), + "a": [1, 1, 1, 2, 2, 2], + "b": pd.Categorical(["a", "a", "b", "b", "c", "c"], ordered=True), + "c": ["x", "x", "y", "z", "x", "y"], + } ) df = mi.to_frame(index=False) df_dtypes = df.dtypes.to_dict() diff --git a/pandas/tests/indexes/test_setops.py b/pandas/tests/indexes/test_setops.py index be61d8b5dda27..a7e2363ec422e 100644 --- a/pandas/tests/indexes/test_setops.py +++ b/pandas/tests/indexes/test_setops.py @@ -15,14 +15,12 @@ from pandas.tests.indexes.conftest import indices_dict import pandas.util.testing as tm -COMPATIBLE_INCONSISTENT_PAIRS = dict( - [ - ((Int64Index, RangeIndex), (tm.makeIntIndex, tm.makeRangeIndex)), - ((Float64Index, Int64Index), (tm.makeFloatIndex, tm.makeIntIndex)), - ((Float64Index, RangeIndex), (tm.makeFloatIndex, tm.makeIntIndex)), - ((Float64Index, UInt64Index), (tm.makeFloatIndex, tm.makeUIntIndex)), - ] -) +COMPATIBLE_INCONSISTENT_PAIRS = { + (Int64Index, RangeIndex): (tm.makeIntIndex, tm.makeRangeIndex), + (Float64Index, Int64Index): (tm.makeFloatIndex, tm.makeIntIndex), + (Float64Index, RangeIndex): (tm.makeFloatIndex, tm.makeIntIndex), + (Float64Index, UInt64Index): (tm.makeFloatIndex, tm.makeUIntIndex), +} @pytest.fixture(params=it.combinations(indices_dict, 2), ids="-".join) From 5ea9cb6dc8997167136ce5d2569818f9b142d671 Mon Sep 17 00:00:00 2001 From: alimcmaster1 Date: Sat, 30 Nov 2019 21:14:41 +0000 Subject: [PATCH 7/7] More literals --- pandas/tests/groupby/aggregate/test_aggregate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 53dda2693dcf6..9543c9d5b59de 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -257,12 +257,12 @@ def test_more_flexible_frame_multi_function(df): grouped = df.groupby("A") exmean = grouped.agg({"C": np.mean, "D": np.mean}) - exstd = grouped.agg({"C", np.std, "D", np.std}) + exstd = grouped.agg({"C": np.std, "D": np.std}) expected = concat([exmean, exstd], keys=["mean", "std"], axis=1) expected = expected.swaplevel(0, 1, axis=1).sort_index(level=0, axis=1) - d = dict([["C", [np.mean, np.std]], ["D", [np.mean, np.std]]]) + d = {"C": [np.mean, np.std], "D": [np.mean, np.std]} result = grouped.aggregate(d) tm.assert_frame_equal(result, expected)