From 9d03b801e0a9ad938c9b2ab0b0945a0aeb960f6a Mon Sep 17 00:00:00 2001 From: JustinZhengBC Date: Sun, 4 Nov 2018 10:31:20 -0800 Subject: [PATCH 1/3] BUG-23451 Allow setting date in string index for Series --- pandas/core/series.py | 3 +++ pandas/tests/series/test_datetime_values.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 6cc5acc4a61d0..662fe1fd72e0a 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -947,6 +947,9 @@ def _set_with(self, key, value): except Exception: pass + if isinstance(key, str): + key = [key] + if not isinstance(key, (list, Series, np.ndarray, Series)): try: key = list(key) diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index 2f6efc112819c..befa4238feeea 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -548,3 +548,9 @@ def test_minmax_nat_series(self, nat): def test_minmax_nat_dataframe(self, nat): assert nat.min()[0] is pd.NaT assert nat.max()[0] is pd.NaT + + def test_set_dt_with_string_index(self): + from datetime import date + x = pd.Series([1, 2, 3], index=['Date', 'b', 'other']) + x.Date = date.today() + assert x.Date == date.today() From ad7c8ef6030efa86d70319e18cfe054f27548606 Mon Sep 17 00:00:00 2001 From: JustinZhengBC Date: Sun, 4 Nov 2018 13:01:29 -0800 Subject: [PATCH 2/3] BUG-23451 Add whatsnew and other requested changes --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/core/series.py | 5 ++--- pandas/tests/series/test_datetime_values.py | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 64cc098ccaa94..600def99ec3f2 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -1115,6 +1115,7 @@ Datetimelike - Bug in :func:`DataFrame.combine` with datetimelike values raising a TypeError (:issue:`23079`) - Bug in :func:`date_range` with frequency of ``Day`` or higher where dates sufficiently far in the future could wrap around to the past instead of raising ``OutOfBoundsDatetime`` (:issue:`14187`) - Bug in :class:`PeriodIndex` with attribute ``freq.n`` greater than 1 where adding a :class:`DateOffset` object would return incorrect results (:issue:`23215`) +- Bug in :class:`Series` that interpreted string indices as lists of characters when setting datetimelike values Timedelta ^^^^^^^^^ diff --git a/pandas/core/series.py b/pandas/core/series.py index 662fe1fd72e0a..58cd0fc4cfc31 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -947,10 +947,9 @@ def _set_with(self, key, value): except Exception: pass - if isinstance(key, str): + if is_scalar(key): key = [key] - - if not isinstance(key, (list, Series, np.ndarray, Series)): + elif not isinstance(key, (list, Series, np.ndarray)): try: key = list(key) except Exception: diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index befa4238feeea..1a4d3c583dd94 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -550,7 +550,8 @@ def test_minmax_nat_dataframe(self, nat): assert nat.max()[0] is pd.NaT def test_set_dt_with_string_index(self): - from datetime import date + # GH 23451 x = pd.Series([1, 2, 3], index=['Date', 'b', 'other']) - x.Date = date.today() + x['Date'] = date.today() assert x.Date == date.today() + assert x['Date'] == date.today() From 70a072570f95e73a3c91bd583b6b18107cb41ba6 Mon Sep 17 00:00:00 2001 From: JustinZhengBC Date: Mon, 5 Nov 2018 13:12:46 -0800 Subject: [PATCH 3/3] BUG-23451 Make requested changes --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/tests/series/test_datetime_values.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 600def99ec3f2..5a87f837a78f3 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -1115,7 +1115,7 @@ Datetimelike - Bug in :func:`DataFrame.combine` with datetimelike values raising a TypeError (:issue:`23079`) - Bug in :func:`date_range` with frequency of ``Day`` or higher where dates sufficiently far in the future could wrap around to the past instead of raising ``OutOfBoundsDatetime`` (:issue:`14187`) - Bug in :class:`PeriodIndex` with attribute ``freq.n`` greater than 1 where adding a :class:`DateOffset` object would return incorrect results (:issue:`23215`) -- Bug in :class:`Series` that interpreted string indices as lists of characters when setting datetimelike values +- Bug in :class:`Series` that interpreted string indices as lists of characters when setting datetimelike values (:issue:`23451`) Timedelta ^^^^^^^^^ diff --git a/pandas/tests/series/test_datetime_values.py b/pandas/tests/series/test_datetime_values.py index 1a4d3c583dd94..f3ae2b1e6ad15 100644 --- a/pandas/tests/series/test_datetime_values.py +++ b/pandas/tests/series/test_datetime_values.py @@ -549,7 +549,7 @@ def test_minmax_nat_dataframe(self, nat): assert nat.min()[0] is pd.NaT assert nat.max()[0] is pd.NaT - def test_set_dt_with_string_index(self): + def test_setitem_with_string_index(self): # GH 23451 x = pd.Series([1, 2, 3], index=['Date', 'b', 'other']) x['Date'] = date.today()