From 98fc2aa624f10e3924e7fc4ee60bbfa1eb37f5d1 Mon Sep 17 00:00:00 2001 From: Dmitrii Gorislavetc Date: Sat, 15 Jul 2017 11:54:15 +0300 Subject: [PATCH 1/4] CLN16668: remove OrderedDefaultDict --- doc/source/whatsnew/v0.21.0.txt | 2 +- pandas/compat/__init__.py | 25 ------------------------- pandas/core/panel.py | 4 ++-- 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index bd19d71182762..1aed10cdb375f 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -129,7 +129,7 @@ Removal of prior version deprecations/changes - ``Categorical`` has dropped the ``.order()`` and ``.sort()`` methods in favor of ``.sort_values()`` (:issue:`12882`) - :func:`eval` and :method:`DataFrame.eval` have changed the default of ``inplace`` from ``None`` to ``False`` (:issue:`11149`) - The function ``get_offset_name`` has been dropped in favor of the ``.freqstr`` attribute for an offset (:issue:`11834`) - +- The class ``OrderedDefaultDict`` has been removed in favor of ``OrderedDict`` (:issue:`16668`) .. _whatsnew_0210.performance: diff --git a/pandas/compat/__init__.py b/pandas/compat/__init__.py index 9eacb9acef2c9..33b41d61aa978 100644 --- a/pandas/compat/__init__.py +++ b/pandas/compat/__init__.py @@ -21,7 +21,6 @@ given metaclass instead (and avoids intermediary class creation) Other items: -* OrderedDefaultDict * platform checker """ # pylint disable=W0611 @@ -373,30 +372,6 @@ def parse_date(timestr, *args, **kwargs): parse_date = _date_parser.parse -class OrderedDefaultdict(OrderedDict): - - def __init__(self, *args, **kwargs): - newdefault = None - newargs = () - if args: - newdefault = args[0] - if not (newdefault is None or callable(newdefault)): - raise TypeError('first argument must be callable or None') - newargs = args[1:] - self.default_factory = newdefault - super(self.__class__, self).__init__(*newargs, **kwargs) - - def __missing__(self, key): - if self.default_factory is None: - raise KeyError(key) - self[key] = value = self.default_factory() - return value - - def __reduce__(self): # optional, for pickle support - args = self.default_factory if self.default_factory else tuple() - return type(self), args, None, None, list(self.items()) - - # https://github.com/pandas-dev/pandas/pull/9123 def is_platform_little_endian(): """ am I little endian """ diff --git a/pandas/core/panel.py b/pandas/core/panel.py index d1f5b4587059c..3b550a0db473c 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -19,7 +19,7 @@ import pandas.core.ops as ops import pandas.core.missing as missing from pandas import compat -from pandas.compat import (map, zip, range, u, OrderedDict, OrderedDefaultdict) +from pandas.compat import (map, zip, range, u, OrderedDict) from pandas.compat.numpy import function as nv from pandas.core.common import _try_sort, _default_index from pandas.core.frame import DataFrame @@ -262,7 +262,7 @@ def from_dict(cls, data, intersect=False, orient='items', dtype=None): """ orient = orient.lower() if orient == 'minor': - new_data = OrderedDefaultdict(dict) + new_data = OrderedDict(dict) for col, df in compat.iteritems(data): for item, s in compat.iteritems(df): new_data[item][col] = s From 2f64621a012f6fd6d33cd7191d6c4646b55ccf89 Mon Sep 17 00:00:00 2001 From: Dmitrii Gorislavetc Date: Sat, 15 Jul 2017 13:22:48 +0300 Subject: [PATCH 2/4] CLN16668: set default value to empty dict at init --- pandas/core/panel.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 3b550a0db473c..c5c1e670cd478 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -262,9 +262,11 @@ def from_dict(cls, data, intersect=False, orient='items', dtype=None): """ orient = orient.lower() if orient == 'minor': - new_data = OrderedDict(dict) + new_data = OrderedDict() for col, df in compat.iteritems(data): for item, s in compat.iteritems(df): + if item not in new_data: + new_data[item] = {} new_data[item][col] = s data = new_data elif orient != 'items': # pragma: no cover From d961703eb625bb7a5b1b3df548a26c12ca6a41a4 Mon Sep 17 00:00:00 2001 From: Dmitrii Gorislavetc Date: Sat, 15 Jul 2017 16:11:59 +0300 Subject: [PATCH 3/4] CLN16668: simplify by using defaultdict instead of handmade value init --- doc/source/whatsnew/v0.21.0.txt | 1 - pandas/core/panel.py | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 1aed10cdb375f..2abd0f53a2654 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -129,7 +129,6 @@ Removal of prior version deprecations/changes - ``Categorical`` has dropped the ``.order()`` and ``.sort()`` methods in favor of ``.sort_values()`` (:issue:`12882`) - :func:`eval` and :method:`DataFrame.eval` have changed the default of ``inplace`` from ``None`` to ``False`` (:issue:`11149`) - The function ``get_offset_name`` has been dropped in favor of the ``.freqstr`` attribute for an offset (:issue:`11834`) -- The class ``OrderedDefaultDict`` has been removed in favor of ``OrderedDict`` (:issue:`16668`) .. _whatsnew_0210.performance: diff --git a/pandas/core/panel.py b/pandas/core/panel.py index c5c1e670cd478..69a8468552f54 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -260,13 +260,13 @@ def from_dict(cls, data, intersect=False, orient='items', dtype=None): ------- Panel """ + from collections import defaultdict + orient = orient.lower() if orient == 'minor': - new_data = OrderedDict() + new_data = defaultdict(OrderedDict) for col, df in compat.iteritems(data): for item, s in compat.iteritems(df): - if item not in new_data: - new_data[item] = {} new_data[item][col] = s data = new_data elif orient != 'items': # pragma: no cover From 1dc417e00ad6338ce181b9636c8e6911363d7f1b Mon Sep 17 00:00:00 2001 From: Dmitrii Gorislavetc Date: Sat, 15 Jul 2017 16:17:40 +0300 Subject: [PATCH 4/4] CLN16668: simplify by using defaultdict instead of handmade value init --- doc/source/whatsnew/v0.21.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 2abd0f53a2654..bd19d71182762 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -130,6 +130,7 @@ Removal of prior version deprecations/changes - :func:`eval` and :method:`DataFrame.eval` have changed the default of ``inplace`` from ``None`` to ``False`` (:issue:`11149`) - The function ``get_offset_name`` has been dropped in favor of the ``.freqstr`` attribute for an offset (:issue:`11834`) + .. _whatsnew_0210.performance: Performance Improvements