-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG GH23451 Allow setting date in string index for Series #23495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Hello @JustinZhengBC! Thanks for submitting the PR.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs a whatsnew note as well
pandas/core/series.py
Outdated
@@ -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)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Series is here twice :< can you remove
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be an elseif
pandas/core/series.py
Outdated
@@ -947,6 +947,9 @@ def _set_with(self, key, value): | |||
except Exception: | |||
pass | |||
|
|||
if isinstance(key, str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you might be able to make this is_scalar
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
imports go at the top
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you try this both as (parametrize)
x.Date =
and x['Date'] =
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't seem to figure out how to parametrize both these cases, as "Date" is being used in different contexts. What would be the variable(s) I parametrize and how would I use it with x
to cover both cases?
Codecov Report
@@ Coverage Diff @@
## master #23495 +/- ##
==========================================
- Coverage 92.25% 92.25% -0.01%
==========================================
Files 161 161
Lines 51176 51178 +2
==========================================
Hits 47214 47214
- Misses 3962 3964 +2
Continue to review full report at Codecov.
|
Can you see if this fixes #12862 as well? |
@mroeschke it fixes the issue of strings being split into lists, but that bug still persists because for tz-aware timestamps, the data is stored in a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small comments, ping on green.
doc/source/whatsnew/v0.24.0.txt
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add the issue number
@@ -548,3 +548,10 @@ 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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to test_setitem_with_string_index
9b45d24
to
ec3e684
Compare
ec3e684
to
70a0725
Compare
lgtm. ping on green. |
@jreback green now |
thanks! |
git diff upstream/master -u -- "*.py" | flake8 --diff
Prevents string indices from turning into lists of characters when setting a date.