From 54c50687ddfcd79814aa1f854056b51eacd4e9e1 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 8 Jul 2022 00:29:21 -0700 Subject: [PATCH 01/36] DOC #45443 edited the documentation of where/mask functions --- pandas/core/generic.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ba3474a2513fb..b46eff137394c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9614,7 +9614,8 @@ def where( The {name} method is an application of the if-then idiom. For each element in the calling DataFrame, if ``cond`` is ``{cond}`` the element is used; otherwise the corresponding element from the DataFrame - ``other`` is used. + ``other`` is used. If `cond` {klass} is less in size than `other`, the default bool + for the missing value is {cond_rev}. The signature for :func:`DataFrame.where` differs from :func:`numpy.where`. Roughly ``df1.where(m, df2)`` is equivalent to @@ -9641,6 +9642,23 @@ def where( 4 NaN dtype: float64 + >>> s = pd.Series(range(5)) + >>> t = pd.Series([True, False]) + >>> s.where(t,99) + 0 0 + 1 99 + 2 99 + 3 99 + 4 99 + dtype: int64 + >>> s.mask(t, 99) + 0 99 + 1 1 + 2 99 + 3 99 + 4 99 + dtype: int64 + >>> s.where(s > 1, 10) 0 10 1 10 From 2951fb14ef8c589f50b5a28e76878de410968b79 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 8 Jul 2022 00:39:29 -0700 Subject: [PATCH 02/36] DOC #45443 edited the documentation of where/mask functions --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b46eff137394c..489ad1e3bf5c2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9614,8 +9614,8 @@ def where( The {name} method is an application of the if-then idiom. For each element in the calling DataFrame, if ``cond`` is ``{cond}`` the element is used; otherwise the corresponding element from the DataFrame - ``other`` is used. If `cond` {klass} is less in size than `other`, the default bool - for the missing value is {cond_rev}. + ``other`` is used. If `cond` {klass} is less in size than `other`, the + default bool for the missing value is {cond_rev}. The signature for :func:`DataFrame.where` differs from :func:`numpy.where`. Roughly ``df1.where(m, df2)`` is equivalent to From 8afd6a1fad45a45326e0fdac46eb5cfd8ffac551 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 8 Jul 2022 08:12:01 -0700 Subject: [PATCH 03/36] Update generic.py --- pandas/core/generic.py | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 489ad1e3bf5c2..ba3474a2513fb 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -9614,8 +9614,7 @@ def where( The {name} method is an application of the if-then idiom. For each element in the calling DataFrame, if ``cond`` is ``{cond}`` the element is used; otherwise the corresponding element from the DataFrame - ``other`` is used. If `cond` {klass} is less in size than `other`, the - default bool for the missing value is {cond_rev}. + ``other`` is used. The signature for :func:`DataFrame.where` differs from :func:`numpy.where`. Roughly ``df1.where(m, df2)`` is equivalent to @@ -9642,23 +9641,6 @@ def where( 4 NaN dtype: float64 - >>> s = pd.Series(range(5)) - >>> t = pd.Series([True, False]) - >>> s.where(t,99) - 0 0 - 1 99 - 2 99 - 3 99 - 4 99 - dtype: int64 - >>> s.mask(t, 99) - 0 99 - 1 1 - 2 99 - 3 99 - 4 99 - dtype: int64 - >>> s.where(s > 1, 10) 0 10 1 10 From 4d43e69b61d0ec6df931a69a50e70c1494624bbc Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 11 Jul 2022 12:13:56 -0700 Subject: [PATCH 04/36] Bug 43767 fix --- pandas/core/resample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 7306d13e44982..fcda7311663cf 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,10 +512,10 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if isinstance(result, ABCSeries) and self._selection is not None: + if (isinstance(result, ABCSeries) or isinstance(result, ABCDataFrame)) and self._selection is not None: result.name = self._selection - if isinstance(result, ABCSeries) and result.empty: + if (isinstance(result, ABCSeries) or isinstance(result, ABCDataFrame)) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not result.index = _asfreq_compat(obj.index[:0], freq=self.freq) From 75fdb7381496d907e9789ff302a21eb5a6c6ea42 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 11 Jul 2022 12:17:43 -0700 Subject: [PATCH 05/36] fixing lines doc --- pandas/core/resample.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index fcda7311663cf..e857091fb5496 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,10 +512,12 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if (isinstance(result, ABCSeries) or isinstance(result, ABCDataFrame)) and self._selection is not None: + if (isinstance(result, ABCSeries) or + isinstance(result, ABCDataFrame)) and self._selection is not None: result.name = self._selection - if (isinstance(result, ABCSeries) or isinstance(result, ABCDataFrame)) and result.empty: + if (isinstance(result, ABCSeries) or + isinstance(result, ABCDataFrame)) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not result.index = _asfreq_compat(obj.index[:0], freq=self.freq) From 26dc5e82dbc1dbd0b67a3a0b2ad7fd172927a24c Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 11 Jul 2022 12:19:37 -0700 Subject: [PATCH 06/36] visual indent --- pandas/core/resample.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index e857091fb5496..886d97d4d191a 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,12 +512,12 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if (isinstance(result, ABCSeries) or - isinstance(result, ABCDataFrame)) and self._selection is not None: + if (isinstance(result, ABCSeries) or + isinstance(result, ABCDataFrame)) and self._selection is not None: result.name = self._selection if (isinstance(result, ABCSeries) or - isinstance(result, ABCDataFrame)) and result.empty: + isinstance(result, ABCDataFrame)) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not result.index = _asfreq_compat(obj.index[:0], freq=self.freq) From 47bcbb307df70be0dd0341c859ff71f10a0ca92b Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 11 Jul 2022 12:20:32 -0700 Subject: [PATCH 07/36] visual indent --- pandas/core/resample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 886d97d4d191a..d1dc4cbce6d74 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -513,11 +513,11 @@ def _wrap_result(self, result): Potentially wrap any results. """ if (isinstance(result, ABCSeries) or - isinstance(result, ABCDataFrame)) and self._selection is not None: + isinstance(result, ABCDataFrame)) and self._selection is not None: result.name = self._selection if (isinstance(result, ABCSeries) or - isinstance(result, ABCDataFrame)) and result.empty: + isinstance(result, ABCDataFrame)) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not result.index = _asfreq_compat(obj.index[:0], freq=self.freq) From 390ff5a3ffe3f4a65c3cc019d4a19886fd903543 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 11 Jul 2022 12:32:15 -0700 Subject: [PATCH 08/36] indentation --- pandas/core/resample.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index d1dc4cbce6d74..6041ef896e859 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,12 +512,14 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if (isinstance(result, ABCSeries) or - isinstance(result, ABCDataFrame)) and self._selection is not None: + if ( + isinstance(result, ABCSeries) or isinstance(result, ABCDataFrame) + ) and self._selection is not None: result.name = self._selection - if (isinstance(result, ABCSeries) or - isinstance(result, ABCDataFrame)) and result.empty: + if ( + isinstance(result, ABCSeries) or isinstance(result, ABCDataFrame) + ) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not result.index = _asfreq_compat(obj.index[:0], freq=self.freq) From e8e51c4f1b6fdf5d8deb316f4fa00e7cf17f9e9a Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 10:50:43 -0700 Subject: [PATCH 09/36] grouby.resample bug --- pandas/tests/resample/test_resampler_grouper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index c54d9de009940..2080a59f72062 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -435,7 +435,7 @@ def test_empty(keys): # GH 26411 df = DataFrame([], columns=["a", "b"], index=TimedeltaIndex([])) result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() - expected = DataFrame(columns=["a", "b"]).set_index(keys, drop=False) + expected = DataFrame(columns=["a", "b"]).set_index(keys.append(np.NaN), drop=False) if len(keys) == 1: expected.index.name = keys[0] From d05f473b3968cea0c62fefa8404d2a47a0f972c6 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 11:51:31 -0700 Subject: [PATCH 10/36] groubby.sample --- pandas/core/resample.py | 2 +- pandas/tests/resample/test_resampler_grouper.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index ec7d6925db72e..58e99166f1180 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -522,7 +522,7 @@ def _wrap_result(self, result): ) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not - result.index = _asfreq_compat(obj.index[:0], freq=self.freq) + result.set_index(_asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True) result.name = getattr(obj, "name", None) return result diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index b122667b70374..7c1b28783e754 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -433,9 +433,13 @@ def test_resample_groupby_agg_listlike(): @pytest.mark.parametrize("keys", [["a"], ["a", "b"]]) def test_empty(keys): # GH 26411 - df = DataFrame([], columns=["a", "b"], index=TimedeltaIndex([])) + df = DataFrame([], columns=["a", "b", "date"]) + df["date"] = pd.to_datetime(df["date"]) + df.set_index("date", inplace=True) result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() - expected = DataFrame(columns=["a", "b"]).set_index(keys.append(np.NaN), drop=False) + expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False) + expected["date"] = pd.to_datetime(expected["date"]) + expected.set_index("date", append=True, drop =True, inplace = True) if len(keys) == 1: expected.index.name = keys[0] From 9c1fc4fd42f041bcf54523a1799cd9f47190532b Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:01:31 -0700 Subject: [PATCH 11/36] syntax --- pandas/core/resample.py | 5 ++++- pandas/tests/resample/test_resampler_grouper.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 58e99166f1180..eed69b8c3f013 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -522,7 +522,10 @@ def _wrap_result(self, result): ) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not - result.set_index(_asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True) + result.set_index ( + _asfreq_compat(obj.index[:0], freq=self.freq), + inplace=True, append=True + ) result.name = getattr(obj, "name", None) return result diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 7c1b28783e754..800cad7eb191b 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -439,7 +439,7 @@ def test_empty(keys): result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False) expected["date"] = pd.to_datetime(expected["date"]) - expected.set_index("date", append=True, drop =True, inplace = True) + expected.set_index("date", append=True, drop=True, inplace=True) if len(keys) == 1: expected.index.name = keys[0] From c6717645c66f1c7c84651555291e33a649eaac83 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:02:57 -0700 Subject: [PATCH 12/36] syntax --- pandas/core/resample.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index eed69b8c3f013..8c1e31af9d6e2 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -522,9 +522,8 @@ def _wrap_result(self, result): ) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not - result.set_index ( - _asfreq_compat(obj.index[:0], freq=self.freq), - inplace=True, append=True + result.set_index( + _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True ) result.name = getattr(obj, "name", None) From cdb642bca8738d468d5de2f3104a0b9ab3cfac7e Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:03:50 -0700 Subject: [PATCH 13/36] syntax --- pandas/core/resample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 8c1e31af9d6e2..755332e1b8d90 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -522,7 +522,7 @@ def _wrap_result(self, result): ) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not - result.set_index( + result.set_index ( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True ) result.name = getattr(obj, "name", None) From 891bc7c8bf66b9928f6c28333077274e2169a91b Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:04:57 -0700 Subject: [PATCH 14/36] syntax --- pandas/core/resample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 755332e1b8d90..8c1e31af9d6e2 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -522,7 +522,7 @@ def _wrap_result(self, result): ) and result.empty: obj = self.obj # When index is all NaT, result is empty but index is not - result.set_index ( + result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True ) result.name = getattr(obj, "name", None) From 34dcf226ecb6cdaf6a697c2bb2a9530124c2668e Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:11:34 -0700 Subject: [PATCH 15/36] what's new --- doc/source/whatsnew/v1.5.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index a6408b940119d..d442fc95ff9fc 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -1001,6 +1001,7 @@ Groupby/resample/rolling - Bug in :meth:`DataFrame.resample` reduction methods when used with ``on`` would attempt to aggregate the provided column (:issue:`47079`) - Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby` would not respect ``dropna=False`` when the input DataFrame/Series had a NaN values in a :class:`MultiIndex` (:issue:`46783`) - Bug in :meth:`DataFrameGroupBy.resample` raises ``KeyError`` when getting the result from a key list which misses the resample key (:issue:`47362`) +- Bug in :meth:`DataFrameGroupBy.resample` produces inconsistent results when passing empty DataFrame (:issue:`47705`) - Reshaping From 2a51ddf67889b5e14a4c3733c71a23e155efc7bf Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 12:21:29 -0700 Subject: [PATCH 16/36] flake8 error --- pandas/tests/resample/test_resampler_grouper.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 800cad7eb191b..7598b82e750b1 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -11,7 +11,6 @@ DataFrame, Index, Series, - TimedeltaIndex, Timestamp, ) import pandas._testing as tm From 1fe7026c177de77ac2f11e710f241e29a83942b3 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 14:11:20 -0700 Subject: [PATCH 17/36] pytest --- pandas/core/resample.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 8c1e31af9d6e2..9339a822f34aa 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,19 +512,19 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if ( - isinstance(result, ABCSeries) or isinstance(result, ABCDataFrame) - ) and self._selection is not None: - result.name = self._selection - - if ( - isinstance(result, ABCSeries) or isinstance(result, ABCDataFrame) - ) and result.empty: + if isinstance(result, ABCDataFrame) and result.empty: obj = self.obj - # When index is all NaT, result is empty but index is not result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True ) + + if isinstance(result, ABCSeries) and self._selection is not None: + result.name = self._selection + + if isinstance(result, ABCSeries) and result.empty: + obj = self.obj + # When index is all NaT, result is empty but index is not + result.index = _asfreq_compat(obj.index[:0], freq=self.freq) result.name = getattr(obj, "name", None) return result From 9d48bfc5ea0b00ef97c4cc59b5a681ba1e35bc62 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 13 Jul 2022 14:12:31 -0700 Subject: [PATCH 18/36] blank line --- pandas/core/resample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 9339a822f34aa..415631d021692 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -517,7 +517,7 @@ def _wrap_result(self, result): result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True ) - + if isinstance(result, ABCSeries) and self._selection is not None: result.name = self._selection From a076f443403a0078d3d315dbd1076a6598a3e1c9 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 15 Jul 2022 16:02:02 -0700 Subject: [PATCH 19/36] editting resample --- pandas/core/resample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 415631d021692..12c17956b4a27 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,7 +512,7 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if isinstance(result, ABCDataFrame) and result.empty: + if isinstance(result, ABCDataFrame) and result.empty and len(result.index) != 0: obj = self.obj result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True From 1450abd104a380007d2bd5fc9c832e46d56c83ba Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 15 Jul 2022 19:31:18 -0700 Subject: [PATCH 20/36] editting resample --- pandas/core/resample.py | 2 +- pandas/tests/resample/test_resampler_grouper.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 12c17956b4a27..d493ac94b894e 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,7 +512,7 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if isinstance(result, ABCDataFrame) and result.empty and len(result.index) != 0: + if isinstance(result, ABCDataFrame) and result.empty and type(result.index) != PeriodIndex: obj = self.obj result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 7598b82e750b1..1a3305a1d1930 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -430,7 +430,7 @@ def test_resample_groupby_agg_listlike(): @pytest.mark.parametrize("keys", [["a"], ["a", "b"]]) -def test_empty(keys): +def test_resample_empty_Dataframe(keys): # GH 26411 df = DataFrame([], columns=["a", "b", "date"]) df["date"] = pd.to_datetime(df["date"]) From 0a45b1fb0ed686e7a415643d510e3194f5645abb Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 15 Jul 2022 19:32:38 -0700 Subject: [PATCH 21/36] syntax --- pandas/core/resample.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index d493ac94b894e..45e6db0e9a480 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,7 +512,8 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if isinstance(result, ABCDataFrame) and result.empty and type(result.index) != PeriodIndex: + if isinstance(result, ABCDataFrame) and + result.empty and type(result.index) != PeriodIndex: obj = self.obj result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True From c04db0e5f0f6ecb15502ee387c4dd7112be3f6b3 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 15 Jul 2022 19:34:25 -0700 Subject: [PATCH 22/36] syntax --- pandas/core/resample.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 45e6db0e9a480..96294241b42da 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,8 +512,10 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if isinstance(result, ABCDataFrame) and - result.empty and type(result.index) != PeriodIndex: + if ( + isinstance(result, ABCDataFrame) and + result.empty and type(result.index) != PeriodIndex + ): obj = self.obj result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True From 3a9070d3e2f0eed58d665eb5f4a44fcfe15551e0 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 15 Jul 2022 20:27:59 -0700 Subject: [PATCH 23/36] syntax --- pandas/core/resample.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 96294241b42da..75b4e5a8baf36 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,9 +512,10 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if ( - isinstance(result, ABCDataFrame) and - result.empty and type(result.index) != PeriodIndex + if( + isinstance(result, ABCDataFrame) + and result.empty + and type(result.index) != PeriodIndex ): obj = self.obj result.set_index( From e683177e6adb7566bbe88204d70983d84f2504b5 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 15 Jul 2022 20:31:55 -0700 Subject: [PATCH 24/36] space --- pandas/core/resample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 75b4e5a8baf36..6cd8ecef1fb73 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,7 +512,7 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ - if( + if ( isinstance(result, ABCDataFrame) and result.empty and type(result.index) != PeriodIndex From b10a311cbd967c723be261d0696a8dfa51685770 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 15 Jul 2022 21:09:59 -0700 Subject: [PATCH 25/36] space --- pandas/core/resample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 6cd8ecef1fb73..d7661476686e0 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -512,12 +512,13 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ + obj = self.obj + if ( isinstance(result, ABCDataFrame) and result.empty and type(result.index) != PeriodIndex ): - obj = self.obj result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True ) @@ -526,7 +527,6 @@ def _wrap_result(self, result): result.name = self._selection if isinstance(result, ABCSeries) and result.empty: - obj = self.obj # When index is all NaT, result is empty but index is not result.index = _asfreq_compat(obj.index[:0], freq=self.freq) result.name = getattr(obj, "name", None) From d3af97b88246fe1c34f1f2c42dafcefb7899ee6c Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 15 Jul 2022 21:11:14 -0700 Subject: [PATCH 26/36] space --- pandas/core/resample.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index d7661476686e0..ab3895e89b5ca 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -513,7 +513,6 @@ def _wrap_result(self, result): Potentially wrap any results. """ obj = self.obj - if ( isinstance(result, ABCDataFrame) and result.empty From 23d37cefc2f2d5598ff14eee7b5e878547d6e093 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 18 Jul 2022 12:44:29 -0700 Subject: [PATCH 27/36] inplace --- pandas/core/resample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index ab3895e89b5ca..6e008ef37a06d 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -518,8 +518,8 @@ def _wrap_result(self, result): and result.empty and type(result.index) != PeriodIndex ): - result.set_index( - _asfreq_compat(obj.index[:0], freq=self.freq), inplace=True, append=True + resule = result.set_index( + _asfreq_compat(obj.index[:0], freq=self.freq), append=True ) if isinstance(result, ABCSeries) and self._selection is not None: From 42f141b9280370b900e1d78289a95e978d57d911 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 18 Jul 2022 12:47:25 -0700 Subject: [PATCH 28/36] spelling --- pandas/core/resample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 6e008ef37a06d..637936242239d 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -518,7 +518,7 @@ def _wrap_result(self, result): and result.empty and type(result.index) != PeriodIndex ): - resule = result.set_index( + result = result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), append=True ) From b54483bfe57f2aa1fc93c51015758f5fe7d84391 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 18 Jul 2022 12:50:14 -0700 Subject: [PATCH 29/36] test --- pandas/tests/resample/test_resampler_grouper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 1a3305a1d1930..5ba2d34e4606c 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -434,11 +434,11 @@ def test_resample_empty_Dataframe(keys): # GH 26411 df = DataFrame([], columns=["a", "b", "date"]) df["date"] = pd.to_datetime(df["date"]) - df.set_index("date", inplace=True) + df = df.set_index("date") result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False) expected["date"] = pd.to_datetime(expected["date"]) - expected.set_index("date", append=True, drop=True, inplace=True) + expected = expected.set_index("date", append=True, drop=True) if len(keys) == 1: expected.index.name = keys[0] From 5e12d91a90193c48fe7aa7b9e6699f5780f0b252 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 10 Aug 2022 20:43:50 -0700 Subject: [PATCH 30/36] test resampler --- .../tests/resample/test_resampler_grouper.py | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 5ba2d34e4606c..06de59953da9e 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -11,6 +11,7 @@ DataFrame, Index, Series, + TimedeltaIndex, Timestamp, ) import pandas._testing as tm @@ -430,15 +431,11 @@ def test_resample_groupby_agg_listlike(): @pytest.mark.parametrize("keys", [["a"], ["a", "b"]]) -def test_resample_empty_Dataframe(keys): +def test_empty(keys): # GH 26411 - df = DataFrame([], columns=["a", "b", "date"]) - df["date"] = pd.to_datetime(df["date"]) - df = df.set_index("date") + df = DataFrame([], columns=["a", "b"], index=TimedeltaIndex([])) result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() - expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False) - expected["date"] = pd.to_datetime(expected["date"]) - expected = expected.set_index("date", append=True, drop=True) + expected = DataFrame(columns=["a", "b"]).set_index(keys, drop=False) if len(keys) == 1: expected.index.name = keys[0] @@ -500,3 +497,19 @@ def test_groupby_resample_with_list_of_keys(): ), ) tm.assert_frame_equal(result, expected) + + +@pytest.mark.parametrize("keys", [["a"], ["a", "b"]]) +def test_resample_empty_Dataframe(keys): + # GH 47705 + df = DataFrame([], columns=["a", "b", "date"]) + df["date"] = pd.to_datetime(df["date"]) + df = df.set_index("date") + result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() + expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False) + expected["date"] = pd.to_datetime(expected["date"]) + expected = expected.set_index("date", append=True, drop=True) + if len(keys) == 1: + expected.index.name = keys[0] + + tm.assert_frame_equal(result, expected) From 2f462ab9ad6429da7bb7390d4b4697e23a3af5f7 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Wed, 10 Aug 2022 21:40:00 -0700 Subject: [PATCH 31/36] tests --- pandas/tests/resample/test_resampler_grouper.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 06de59953da9e..084e1d6a66619 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -435,7 +435,9 @@ def test_empty(keys): # GH 26411 df = DataFrame([], columns=["a", "b"], index=TimedeltaIndex([])) result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() - expected = DataFrame(columns=["a", "b"]).set_index(keys, drop=False) + expected = DataFrame(columns=["a", "b"]).set_index(keys, drop=False).set_index( + TimedeltaIndex([]), append=True + ) if len(keys) == 1: expected.index.name = keys[0] From 727a1c74570c723d1932f4f5bb9f8b2de11b14b7 Mon Sep 17 00:00:00 2001 From: ahmedibrhm <81244897+ahmedibrhm@users.noreply.github.com> Date: Sat, 13 Aug 2022 10:56:46 -0700 Subject: [PATCH 32/36] tests --- pandas/core/resample.py | 2 +- pandas/tests/resample/test_resampler_grouper.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 08ee68f0c1451..a58b89afce852 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -517,7 +517,7 @@ def _wrap_result(self, result): if ( isinstance(result, ABCDataFrame) and result.empty - and type(result.index) != PeriodIndex + and not isinstance(result.index, PeriodIndex) ): result = result.set_index( _asfreq_compat(obj.index[:0], freq=self.freq), append=True diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 084e1d6a66619..904b0e69c9a7d 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -435,8 +435,10 @@ def test_empty(keys): # GH 26411 df = DataFrame([], columns=["a", "b"], index=TimedeltaIndex([])) result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean() - expected = DataFrame(columns=["a", "b"]).set_index(keys, drop=False).set_index( - TimedeltaIndex([]), append=True + expected = ( + DataFrame(columns=["a", "b"]) + .set_index(keys, drop=False) + .set_index(TimedeltaIndex([]), append=True) ) if len(keys) == 1: expected.index.name = keys[0] From 0b01594a07d4b20b267753180205db0077a209cd Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 15 Aug 2022 10:23:59 -0700 Subject: [PATCH 33/36] Update resample.py --- pandas/core/resample.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index c8ee33db7ea20..aea08790da9d4 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -514,6 +514,7 @@ def _wrap_result(self, result): """ Potentially wrap any results. """ + # GH 47705 obj = self.obj if ( isinstance(result, ABCDataFrame) From 25e7cfd11f4dd74532cff8c77080189571908766 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 15 Aug 2022 10:39:43 -0700 Subject: [PATCH 34/36] Update resample.py --- pandas/core/resample.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index aea08790da9d4..02543bc1dca5c 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -515,6 +515,7 @@ def _wrap_result(self, result): Potentially wrap any results. """ # GH 47705 + obj = self.obj if ( isinstance(result, ABCDataFrame) From 900933b492c837fd862af963d6955cce93121116 Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim <81244897+ahmedibrhm@users.noreply.github.com> Date: Mon, 15 Aug 2022 10:40:12 -0700 Subject: [PATCH 35/36] Update resample.py --- pandas/core/resample.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 02543bc1dca5c..aea08790da9d4 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -515,7 +515,6 @@ def _wrap_result(self, result): Potentially wrap any results. """ # GH 47705 - obj = self.obj if ( isinstance(result, ABCDataFrame) From c178d4dd6112b3263b8567b6c4f6f913f4ea800e Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim <81244897+ahmedibrhm@users.noreply.github.com> Date: Fri, 30 Sep 2022 22:59:41 +0800 Subject: [PATCH 36/36] Update v1.6.0.rst --- doc/source/whatsnew/v1.6.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index d726f69286469..a83f8a01343ab 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -238,6 +238,7 @@ Plotting Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ - Bug in :meth:`DataFrameGroupBy.sample` raises ``ValueError`` when the object is empty (:issue:`48459`) +- Bug in :meth:`DataFrameGroupBy.resample` produces inconsistent results when passing empty DataFrame (:issue:`47705`) - Reshaping