From 5f05fdcfbfe901fb6401554dd1567b50ebd72d3a Mon Sep 17 00:00:00 2001 From: Nate Yoder Date: Thu, 15 Dec 2016 22:22:42 -0800 Subject: [PATCH 1/2] Fix usage of fast_multiget with index which was always throwing an exception that was then caught; add ASV that show slight improvement --- asv_bench/benchmarks/series_methods.py | 15 +++++++++++++++ pandas/core/series.py | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/asv_bench/benchmarks/series_methods.py b/asv_bench/benchmarks/series_methods.py index 4e368c6d7cde2..c164f3ddb2d6f 100644 --- a/asv_bench/benchmarks/series_methods.py +++ b/asv_bench/benchmarks/series_methods.py @@ -15,6 +15,21 @@ def time_series_constructor_no_data_datetime_index(self): Series(data=None, index=self.dr) +class series_constructor_dict_data_datetime_index(object): + goal_time = 0.2 + + def setup(self): + self.dr = pd.date_range( + start=datetime(2015, 10, 26), + end=datetime(2016, 1, 1), + freq='10s' + ) # ~500k long + self.data = {d: v for d, v in zip(self.dr, range(len(self.dr)))} + + def time_series_constructor_no_data_datetime_index(self): + Series(data=self.data, index=self.dr) + + class series_isin_int64(object): goal_time = 0.2 diff --git a/pandas/core/series.py b/pandas/core/series.py index 7018865e5b3ec..9a8039828c05b 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -188,7 +188,7 @@ def __init__(self, data=None, index=None, dtype=None, name=None, if len(data): # coerce back to datetime objects for lookup data = _dict_compat(data) - data = lib.fast_multiget(data, index.astype('O'), + data = lib.fast_multiget(data, index.asobject.values, default=np.nan) else: data = np.nan From 56be091356c9df8e42cb25d313b1f28e2c01f1dc Mon Sep 17 00:00:00 2001 From: Nate Yoder Date: Fri, 16 Dec 2016 08:30:25 -0800 Subject: [PATCH 2/2] Update whatsnew and fix pep8 issue --- doc/source/whatsnew/v0.19.2.txt | 1 + pandas/core/series.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.19.2.txt b/doc/source/whatsnew/v0.19.2.txt index 82d43db667550..d862f734dfb85 100644 --- a/doc/source/whatsnew/v0.19.2.txt +++ b/doc/source/whatsnew/v0.19.2.txt @@ -22,6 +22,7 @@ Performance Improvements ~~~~~~~~~~~~~~~~~~~~~~~~ - Improved performance of ``.replace()`` (:issue:`12745`) +- Improved performance ``Series`` creation with a datetime index and dictionary data (:issue:`14894`) .. _whatsnew_0192.enhancements.other: diff --git a/pandas/core/series.py b/pandas/core/series.py index 9a8039828c05b..f656d72296e3a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -188,7 +188,8 @@ def __init__(self, data=None, index=None, dtype=None, name=None, if len(data): # coerce back to datetime objects for lookup data = _dict_compat(data) - data = lib.fast_multiget(data, index.asobject.values, + data = lib.fast_multiget(data, + index.asobject.values, default=np.nan) else: data = np.nan