Skip to content

BUG: parse_time_string with np.str_ obj #45626

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

Merged
merged 2 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ Indexing
- Bug in :meth:`Series.__setitem__` when setting ``boolean`` dtype values containing ``NA`` incorrectly raising instead of casting to ``boolean`` dtype (:issue:`45462`)
- Bug in :meth:`Series.__setitem__` where setting :attr:`NA` into a numeric-dtpye :class:`Series` would incorrectly upcast to object-dtype rather than treating the value as ``np.nan`` (:issue:`44199`)
- Bug in getting a column from a DataFrame with an object-dtype row index with datetime-like values: the resulting Series now preserves the exact object-dtype Index from the parent DataFrame (:issue:`42950`)
- Bug in indexing on a :class:`DatetimeIndex` with a ``np.str_`` key incorrectly raising (:issue:`45580`)
-

Missing
Expand Down
12 changes: 11 additions & 1 deletion pandas/_libs/tslibs/parsing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def parse_datetime_string(
return dt


def parse_time_string(arg: str, freq=None, dayfirst=None, yearfirst=None):
def parse_time_string(arg, freq=None, dayfirst=None, yearfirst=None):
"""
Try hard to parse datetime string, leveraging dateutil plus some extra
goodies like quarter recognition.
Expand All @@ -312,6 +312,16 @@ def parse_time_string(arg: str, freq=None, dayfirst=None, yearfirst=None):
str
Describing resolution of parsed string.
"""
if type(arg) is not str:
# GH#45580 np.str_ satisfies isinstance(obj, str) but if we annotate
# arg as "str" this raises here
if not isinstance(arg, np.str_):
raise TypeError(
"Argument 'arg' has incorrect type "
f"(expected str, got {type(arg).__name__})"
)
arg = str(arg)

if is_offset_object(freq):
freq = freq.rule_code

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ def test_loc_getitem_single_boolean_arg(self, obj, key, exp):

class TestLocBaseIndependent:
# Tests for loc that do not depend on subclassing Base
def test_loc_npstr(self):
# GH#45580
df = DataFrame(index=date_range("2021", "2022"))
result = df.loc[np.array(["2021/6/1"])[0] :]
expected = df.iloc[151:]
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"msg, key",
[
Expand Down