From 676a4e5cef1cf37704ef702699db1fd6c89028ea Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Mon, 20 Mar 2017 19:32:02 -0300 Subject: [PATCH 01/15] Test --- RELEASE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE.md b/RELEASE.md index a181412be2719..efd075dabcba9 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,6 +1,6 @@ Release Notes ============= -The list of changes to pandas between each release can be found +The list of changes to Pandas between each release can be found [here](http://pandas.pydata.org/pandas-docs/stable/whatsnew.html). For full details, see the commit logs at http://github.com/pandas-dev/pandas. From c78d6878b283746cd3155458d107d076f594bfff Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Mon, 20 Mar 2017 23:48:24 -0300 Subject: [PATCH 02/15] BUG #15713 Series.asof return nan when series is all nans --- pandas/core/generic.py | 3 +++ pandas/indexes/base.py | 1 + pandas/tests/series/test_asof.py | 10 ++++++++++ 3 files changed, 14 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1db9677659ca3..be04fc7605be0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3971,6 +3971,9 @@ def asof(self, where, subset=None): if not isinstance(where, Index): where = Index(where) if is_list else Index([where]) + if self.isnull().all(): + return pd.Series([np.nan]) + nulls = self.isnull() if is_series else self[subset].isnull().any(1) locs = self.index.asof_locs(where, ~(nulls.values)) diff --git a/pandas/indexes/base.py b/pandas/indexes/base.py index d262ecd818f1d..c7a21e1ebff57 100644 --- a/pandas/indexes/base.py +++ b/pandas/indexes/base.py @@ -1890,6 +1890,7 @@ def asof_locs(self, where, mask): locs = self.values[mask].searchsorted(where.values, side='right') locs = np.where(locs > 0, locs - 1, 0) + result = np.arange(len(self))[mask].take(locs) first = mask.argmax() diff --git a/pandas/tests/series/test_asof.py b/pandas/tests/series/test_asof.py index d2fd8858e7647..0a467df91604d 100644 --- a/pandas/tests/series/test_asof.py +++ b/pandas/tests/series/test_asof.py @@ -4,6 +4,8 @@ from pandas import (offsets, Series, notnull, isnull, date_range, Timestamp) +from pandas.util.testing import assert_frame_equal, assert_series_equal + import pandas.util.testing as tm from .common import TestData @@ -148,3 +150,11 @@ def test_errors(self): s = Series(np.random.randn(N), index=rng) with self.assertRaises(ValueError): s.asof(s.index[0], subset='foo') + + # series is all nans + result = Series([np.nan]).asof([0]) + + expected = Series([np.nan]) + + assert_series_equal(result, expected) + From 4e26ab89e49d796caa1669ced168547f256e0986 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Mon, 20 Mar 2017 23:59:25 -0300 Subject: [PATCH 03/15] BUG #15713 Series.asof return nan when series is all nans. --- RELEASE.md | 2 +- pandas/indexes/base.py | 1 - pandas/tests/series/test_asof.py | 2 -- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index efd075dabcba9..a181412be2719 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,6 +1,6 @@ Release Notes ============= -The list of changes to Pandas between each release can be found +The list of changes to pandas between each release can be found [here](http://pandas.pydata.org/pandas-docs/stable/whatsnew.html). For full details, see the commit logs at http://github.com/pandas-dev/pandas. diff --git a/pandas/indexes/base.py b/pandas/indexes/base.py index c7a21e1ebff57..d262ecd818f1d 100644 --- a/pandas/indexes/base.py +++ b/pandas/indexes/base.py @@ -1890,7 +1890,6 @@ def asof_locs(self, where, mask): locs = self.values[mask].searchsorted(where.values, side='right') locs = np.where(locs > 0, locs - 1, 0) - result = np.arange(len(self))[mask].take(locs) first = mask.argmax() diff --git a/pandas/tests/series/test_asof.py b/pandas/tests/series/test_asof.py index 0a467df91604d..30f3f31bc1528 100644 --- a/pandas/tests/series/test_asof.py +++ b/pandas/tests/series/test_asof.py @@ -153,8 +153,6 @@ def test_errors(self): # series is all nans result = Series([np.nan]).asof([0]) - expected = Series([np.nan]) - assert_series_equal(result, expected) From 17d1d778822b5304a8b1fadad8649ae408466799 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Tue, 21 Mar 2017 00:01:35 -0300 Subject: [PATCH 04/15] BUG #15713 Series.asof return nan when series is all nans! --- pandas/tests/series/test_asof.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/series/test_asof.py b/pandas/tests/series/test_asof.py index 30f3f31bc1528..946f238359145 100644 --- a/pandas/tests/series/test_asof.py +++ b/pandas/tests/series/test_asof.py @@ -4,7 +4,7 @@ from pandas import (offsets, Series, notnull, isnull, date_range, Timestamp) -from pandas.util.testing import assert_frame_equal, assert_series_equal +from pandas.util.testing import assert_series_equal import pandas.util.testing as tm @@ -155,4 +155,3 @@ def test_errors(self): result = Series([np.nan]).asof([0]) expected = Series([np.nan]) assert_series_equal(result, expected) - From 89fb6cfd26aa243c90f1e704b3de72b4ea26fe85 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Tue, 21 Mar 2017 00:30:21 -0300 Subject: [PATCH 05/15] BUG #15713 fixing failing tests --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index be04fc7605be0..d431c9cfd56ac 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3971,7 +3971,7 @@ def asof(self, where, subset=None): if not isinstance(where, Index): where = Index(where) if is_list else Index([where]) - if self.isnull().all(): + if self.isnull().values.all(): return pd.Series([np.nan]) nulls = self.isnull() if is_series else self[subset].isnull().any(1) From 6b745afc3292020bd7169091fc79e1e00d014932 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Tue, 21 Mar 2017 20:45:54 -0300 Subject: [PATCH 06/15] Adding DataFrame tests & support, and optimizing the code --- pandas/core/generic.py | 9 ++++++--- pandas/tests/frame/test_asof.py | 17 +++++++++++------ pandas/tests/series/test_asof.py | 5 ++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d431c9cfd56ac..d281759cb35a7 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3971,10 +3971,13 @@ def asof(self, where, subset=None): if not isinstance(where, Index): where = Index(where) if is_list else Index([where]) - if self.isnull().values.all(): - return pd.Series([np.nan]) - nulls = self.isnull() if is_series else self[subset].isnull().any(1) + if nulls.values.all(): + if is_series: + return pd.Series([np.nan]) + else: + return pd.DataFrame([np.nan]) + locs = self.index.asof_locs(where, ~(nulls.values)) # mask the missing diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index 8bb26d3d7474c..43c0cbb1c1283 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -4,7 +4,6 @@ from pandas import (DataFrame, date_range, Timestamp, Series, to_datetime) -from pandas.util.testing import assert_frame_equal, assert_series_equal import pandas.util.testing as tm from .common import TestData @@ -51,19 +50,19 @@ def test_subset(self): # with a subset of A should be the same result = df.asof(dates, subset='A') expected = df.asof(dates) - assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # same with A/B result = df.asof(dates, subset=['A', 'B']) expected = df.asof(dates) - assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # B gives self.df.asof result = df.asof(dates, subset='B') expected = df.resample('25s', closed='right').ffill().reindex(dates) expected.iloc[20:] = 9 - assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_missing(self): # GH 15118 @@ -75,9 +74,15 @@ def test_missing(self): result = df.asof('1989-12-31') expected = Series(index=['A', 'B'], name=Timestamp('1989-12-31')) - assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) result = df.asof(to_datetime(['1989-12-31'])) expected = DataFrame(index=to_datetime(['1989-12-31']), columns=['A', 'B'], dtype='float64') - assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) + + def test_all_nans(self): + # series is all nans + result = DataFrame([np.nan]).asof([0]) + expected = DataFrame([np.nan]) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/test_asof.py b/pandas/tests/series/test_asof.py index 946f238359145..7e45549ffdded 100644 --- a/pandas/tests/series/test_asof.py +++ b/pandas/tests/series/test_asof.py @@ -4,8 +4,6 @@ from pandas import (offsets, Series, notnull, isnull, date_range, Timestamp) -from pandas.util.testing import assert_series_equal - import pandas.util.testing as tm from .common import TestData @@ -151,7 +149,8 @@ def test_errors(self): with self.assertRaises(ValueError): s.asof(s.index[0], subset='foo') + def test_all_nans(self): # series is all nans result = Series([np.nan]).asof([0]) expected = Series([np.nan]) - assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) From 70c958f561c8eb7d2e6cd088f111d4d13bc4c96a Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Thu, 23 Mar 2017 18:36:37 -0300 Subject: [PATCH 07/15] Added tests for non-default indexes, scalar and multiple inputs, and results preserve columns --- doc/source/whatsnew/v0.20.0.txt | 2 ++ pandas/core/generic.py | 6 +++--- pandas/tests/frame/test_asof.py | 22 ++++++++++++++++++++-- pandas/tests/series/test_asof.py | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index ebdd4060f0588..5a3b99d6bec0d 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -930,3 +930,5 @@ Bug Fixes - Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`) - Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`) - Bug in ``pd.read_msgpack`` which did not allow to load dataframe with an index of type ``CategoricalIndex`` (:issue:`15487`) + +- Bug in ``Series.asof`` which raised an error if the series contained all ``nans`` (:issue:`15713`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d281759cb35a7..308c180ce665a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3972,11 +3972,11 @@ def asof(self, where, subset=None): where = Index(where) if is_list else Index([where]) nulls = self.isnull() if is_series else self[subset].isnull().any(1) - if nulls.values.all(): + if nulls.all(): if is_series: - return pd.Series([np.nan]) + return pd.Series(np.nan, index=where) else: - return pd.DataFrame([np.nan]) + return pd.DataFrame(np.nan, index=where, columns=self.columns) locs = self.index.asof_locs(where, ~(nulls.values)) diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index 43c0cbb1c1283..f84e08b503f1c 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -13,9 +13,9 @@ class TestFrameAsof(TestData, tm.TestCase): def setUp(self): self.N = N = 50 - rng = date_range('1/1/1990', periods=N, freq='53s') + self.rng = date_range('1/1/1990', periods=N, freq='53s') self.df = DataFrame({'A': np.arange(N), 'B': np.arange(N)}, - index=rng) + index=self.rng) def test_basic(self): @@ -86,3 +86,21 @@ def test_all_nans(self): result = DataFrame([np.nan]).asof([0]) expected = DataFrame([np.nan]) tm.assert_frame_equal(result, expected) + + # testing non-default indexes, multiple inputs + dates = date_range('1/1/1990', periods=self.N * 3, freq='25s') + result = DataFrame(np.nan, index=self.rng, columns=['A']).asof(dates) + expected = DataFrame(np.nan, index=dates, columns=['A']) + tm.assert_frame_equal(result, expected) + + # testing multiple columns + dates = date_range('1/1/1990', periods=self.N * 3, freq='25s') + result = DataFrame(np.nan, index=self.rng, columns=['A', 'B', 'C']).asof(dates) + expected = DataFrame(np.nan, index=dates, columns=['A', 'B', 'C']) + tm.assert_frame_equal(result, expected) + + # testing scalar input + date = date_range('1/1/1990', periods=self.N * 3, freq='25s')[0] + result = DataFrame(np.nan, index=self.rng, columns=['A']).asof(date) + expected = DataFrame(np.nan, index=[date], columns=['A']) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/test_asof.py b/pandas/tests/series/test_asof.py index 7e45549ffdded..a8864b5b9bb47 100644 --- a/pandas/tests/series/test_asof.py +++ b/pandas/tests/series/test_asof.py @@ -154,3 +154,17 @@ def test_all_nans(self): result = Series([np.nan]).asof([0]) expected = Series([np.nan]) tm.assert_series_equal(result, expected) + + # testing non-default indexes + N = 50 + rng = date_range('1/1/1990', periods=N, freq='53s') + + dates = date_range('1/1/1990', periods=N * 3, freq='25s') + result = Series(np.nan, index=rng).asof(dates) + expected = Series(np.nan, index=dates) + tm.assert_series_equal(result, expected) + + # testing scalar input + date = date_range('1/1/1990', periods=N * 3, freq='25s')[0] + result = Series(np.nan, index=rng).asof(date) + self.assertTrue(result != result) From 3f9c7fd5edbdfbad268ccdfb2cb201679f7278c7 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Thu, 23 Mar 2017 19:28:19 -0300 Subject: [PATCH 08/15] Minor comments --- pandas/core/generic.py | 2 +- pandas/tests/frame/test_asof.py | 3 ++- pandas/tests/series/test_asof.py | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 308c180ce665a..d1148f8822e6a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3972,7 +3972,7 @@ def asof(self, where, subset=None): where = Index(where) if is_list else Index([where]) nulls = self.isnull() if is_series else self[subset].isnull().any(1) - if nulls.all(): + if nulls.values.all(): if is_series: return pd.Series(np.nan, index=where) else: diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index f84e08b503f1c..0ba1b0836faf6 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -82,7 +82,8 @@ def test_missing(self): tm.assert_frame_equal(result, expected) def test_all_nans(self): - # series is all nans + # GH 15713 + # DataFrame is all nans result = DataFrame([np.nan]).asof([0]) expected = DataFrame([np.nan]) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/series/test_asof.py b/pandas/tests/series/test_asof.py index a8864b5b9bb47..4b9a2cf160910 100644 --- a/pandas/tests/series/test_asof.py +++ b/pandas/tests/series/test_asof.py @@ -150,6 +150,7 @@ def test_errors(self): s.asof(s.index[0], subset='foo') def test_all_nans(self): + # GH 15713 # series is all nans result = Series([np.nan]).asof([0]) expected = Series([np.nan]) @@ -167,4 +168,4 @@ def test_all_nans(self): # testing scalar input date = date_range('1/1/1990', periods=N * 3, freq='25s')[0] result = Series(np.nan, index=rng).asof(date) - self.assertTrue(result != result) + assert isnull(result) From 04b7306f92b6f7e1a9ae63dc87ae986542cb0575 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Thu, 23 Mar 2017 22:21:19 -0300 Subject: [PATCH 09/15] Removing .values and formating code PEP8 --- pandas/core/generic.py | 2 +- pandas/tests/frame/test_asof.py | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d1148f8822e6a..308c180ce665a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3972,7 +3972,7 @@ def asof(self, where, subset=None): where = Index(where) if is_list else Index([where]) nulls = self.isnull() if is_series else self[subset].isnull().any(1) - if nulls.values.all(): + if nulls.all(): if is_series: return pd.Series(np.nan, index=where) else: diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index 0ba1b0836faf6..d2b47093fa5df 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -10,7 +10,6 @@ class TestFrameAsof(TestData, tm.TestCase): - def setUp(self): self.N = N = 50 self.rng = date_range('1/1/1990', periods=N, freq='53s') @@ -18,7 +17,6 @@ def setUp(self): index=self.rng) def test_basic(self): - df = self.df.copy() df.loc[15:30, 'A'] = np.nan dates = date_range('1/1/1990', periods=self.N * 3, @@ -38,7 +36,6 @@ def test_basic(self): self.assertTrue((rs == 14).all(1).all()) def test_subset(self): - N = 10 rng = date_range('1/1/1990', periods=N, freq='53s') df = DataFrame({'A': np.arange(N), 'B': np.arange(N)}, @@ -96,7 +93,8 @@ def test_all_nans(self): # testing multiple columns dates = date_range('1/1/1990', periods=self.N * 3, freq='25s') - result = DataFrame(np.nan, index=self.rng, columns=['A', 'B', 'C']).asof(dates) + result = DataFrame(np.nan, index=self.rng, + columns=['A', 'B', 'C']).asof(dates) expected = DataFrame(np.nan, index=dates, columns=['A', 'B', 'C']) tm.assert_frame_equal(result, expected) From a080b9bf03f7b1a19968919f5864b4d759297ef4 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Fri, 24 Mar 2017 22:07:34 -0300 Subject: [PATCH 10/15] Making scalar input return in a Series --- pandas/core/generic.py | 6 +++++- pandas/tests/frame/test_asof.py | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 308c180ce665a..238e90c59901d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3976,7 +3976,11 @@ def asof(self, where, subset=None): if is_series: return pd.Series(np.nan, index=where) else: - return pd.DataFrame(np.nan, index=where, columns=self.columns) + if is_list: + return pd.DataFrame(np.nan, index=where, + columns=self.columns) + else: + return pd.Series(np.nan, index=[where]) locs = self.index.asof_locs(where, ~(nulls.values)) diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index d2b47093fa5df..514bd3a6303c1 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -101,5 +101,5 @@ def test_all_nans(self): # testing scalar input date = date_range('1/1/1990', periods=self.N * 3, freq='25s')[0] result = DataFrame(np.nan, index=self.rng, columns=['A']).asof(date) - expected = DataFrame(np.nan, index=[date], columns=['A']) - tm.assert_frame_equal(result, expected) + expected = Series(np.nan, index=[date]) + tm.assert_series_equal(result, expected) From 7448b96cd85f9af85e1e20390008b020a9e6040d Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Sat, 25 Mar 2017 11:24:41 -0300 Subject: [PATCH 11/15] Fixing scalar input --- pandas/core/generic.py | 2 +- pandas/tests/frame/test_asof.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 238e90c59901d..bff5b037520ee 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3980,7 +3980,7 @@ def asof(self, where, subset=None): return pd.DataFrame(np.nan, index=where, columns=self.columns) else: - return pd.Series(np.nan, index=[where]) + return pd.Series(np.nan, index=self.columns) locs = self.index.asof_locs(where, ~(nulls.values)) diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index 514bd3a6303c1..0654042e0f0fc 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -99,7 +99,16 @@ def test_all_nans(self): tm.assert_frame_equal(result, expected) # testing scalar input - date = date_range('1/1/1990', periods=self.N * 3, freq='25s')[0] - result = DataFrame(np.nan, index=self.rng, columns=['A']).asof(date) - expected = Series(np.nan, index=[date]) + result = DataFrame(np.nan, index=[1, 2], columns=['A', 'B']).asof([3]) + expected = DataFrame(np.nan, index=[3], columns=['A', 'B']) + tm.assert_frame_equal(result, expected) + + result = DataFrame(np.nan, index=[1, 2], columns=['A', 'B']).asof(3) + expected = Series(np.nan, index=['A', 'B']) tm.assert_series_equal(result, expected) + + + + + + From b8f078abfe63efe4afc82fdade0c109a31cc66e0 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Sat, 25 Mar 2017 11:27:29 -0300 Subject: [PATCH 12/15] Small code standard change --- pandas/tests/frame/test_asof.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index 0654042e0f0fc..bd32c9b55291d 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -106,9 +106,3 @@ def test_all_nans(self): result = DataFrame(np.nan, index=[1, 2], columns=['A', 'B']).asof(3) expected = Series(np.nan, index=['A', 'B']) tm.assert_series_equal(result, expected) - - - - - - From af9a29b41f0fe9cfb525867de49a1d085f74a6a4 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Sat, 25 Mar 2017 15:07:55 -0300 Subject: [PATCH 13/15] Setting name of asof result when scalar input and all nan --- pandas/core/generic.py | 2 +- pandas/tests/frame/test_asof.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index bff5b037520ee..5b5b0d70f6b2a 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3980,7 +3980,7 @@ def asof(self, where, subset=None): return pd.DataFrame(np.nan, index=where, columns=self.columns) else: - return pd.Series(np.nan, index=self.columns) + return pd.Series(np.nan, index=self.columns, name=where[0]) locs = self.index.asof_locs(where, ~(nulls.values)) diff --git a/pandas/tests/frame/test_asof.py b/pandas/tests/frame/test_asof.py index bd32c9b55291d..dd03f8f7cb7a9 100644 --- a/pandas/tests/frame/test_asof.py +++ b/pandas/tests/frame/test_asof.py @@ -104,5 +104,5 @@ def test_all_nans(self): tm.assert_frame_equal(result, expected) result = DataFrame(np.nan, index=[1, 2], columns=['A', 'B']).asof(3) - expected = Series(np.nan, index=['A', 'B']) + expected = Series(np.nan, index=['A', 'B'], name=3) tm.assert_series_equal(result, expected) From bb63964f5d5f149cc168d5f16230a9a63085c0fa Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Sat, 25 Mar 2017 16:36:11 -0300 Subject: [PATCH 14/15] Propagating Series name --- pandas/core/generic.py | 2 +- pandas/tests/series/test_asof.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5b5b0d70f6b2a..8bd62faacdcad 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3974,7 +3974,7 @@ def asof(self, where, subset=None): nulls = self.isnull() if is_series else self[subset].isnull().any(1) if nulls.all(): if is_series: - return pd.Series(np.nan, index=where) + return pd.Series(np.nan, index=where, name=self.name) else: if is_list: return pd.DataFrame(np.nan, index=where, diff --git a/pandas/tests/series/test_asof.py b/pandas/tests/series/test_asof.py index 4b9a2cf160910..ee6ab15b8963a 100644 --- a/pandas/tests/series/test_asof.py +++ b/pandas/tests/series/test_asof.py @@ -169,3 +169,7 @@ def test_all_nans(self): date = date_range('1/1/1990', periods=N * 3, freq='25s')[0] result = Series(np.nan, index=rng).asof(date) assert isnull(result) + + # test name is propagated + result = Series(np.nan, index=[1, 2, 3, 4], name='test').asof([4, 5]) + self.assertEqual(result.name, 'test') From 076510845f5d00e95d4749fd4db4325f4643fa78 Mon Sep 17 00:00:00 2001 From: Carlos Souza Date: Sat, 25 Mar 2017 19:43:37 -0300 Subject: [PATCH 15/15] First simplification, code-block in the same place --- pandas/core/generic.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8bd62faacdcad..7ca3d10b7977f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3975,12 +3975,10 @@ def asof(self, where, subset=None): if nulls.all(): if is_series: return pd.Series(np.nan, index=where, name=self.name) + elif is_list: + return pd.DataFrame(np.nan, index=where, columns=self.columns) else: - if is_list: - return pd.DataFrame(np.nan, index=where, - columns=self.columns) - else: - return pd.Series(np.nan, index=self.columns, name=where[0]) + return pd.Series(np.nan, index=self.columns, name=where[0]) locs = self.index.asof_locs(where, ~(nulls.values))