Skip to content

Certain comparison operations misbehaving for period dtype #28980

Closed
@dsaxton

Description

@dsaxton

This is closely related to #28930.

import pandas as pd

s = pd.Series([pd.Period("2019"), pd.Period("2020")], dtype="period[A-DEC]") 
s == "a"                                                                                                                                                     

yields

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-3a654234a428> in <module>
----> 1 s == "a"

~/pandas/pandas/core/ops/__init__.py in wrapper(self, other)
    527         rvalues = extract_array(other, extract_numpy=True)
    528 
--> 529         res_values = comparison_op(lvalues, rvalues, op)
    530 
    531         return _construct_result(self, res_values, index=self.index, name=res_name)

~/pandas/pandas/core/ops/array_ops.py in comparison_op(left, right, op)
    253 
    254     if should_extension_dispatch(lvalues, rvalues):
--> 255         res_values = dispatch_to_extension_op(op, lvalues, rvalues)
    256 
    257     elif is_scalar(rvalues) and isna(rvalues):

~/pandas/pandas/core/ops/dispatch.py in dispatch_to_extension_op(op, left, right, keep_null_freq)
    134 
    135     try:
--> 136         res_values = op(left, right)
    137     except NullFrequencyError:
    138         # DatetimeIndex and TimedeltaIndex with freq == None raise ValueError

~/pandas/pandas/core/arrays/period.py in wrapper(self, other)
     98             result.fill(nat_result)
     99         else:
--> 100             other = Period(other, freq=self.freq)
    101             result = ordinal_op(other.ordinal)
    102 

~/pandas/pandas/_libs/tslibs/period.pyx in pandas._libs.tslibs.period.Period.__new__()
   2459                 value = str(value)
   2460             value = value.upper()
-> 2461             dt, _, reso = parse_time_string(value, freq)
   2462             if dt is NaT:
   2463                 ordinal = NPY_NAT

~/pandas/pandas/_libs/tslibs/parsing.pyx in pandas._libs.tslibs.parsing.parse_time_string()
    265             yearfirst = get_option("display.date_yearfirst")
    266 
--> 267     res = parse_datetime_string_with_reso(arg, freq=freq,
    268                                           dayfirst=dayfirst,
    269                                           yearfirst=yearfirst)

~/pandas/pandas/_libs/tslibs/parsing.pyx in pandas._libs.tslibs.parsing.parse_datetime_string_with_reso()
    291 
    292     if not _does_string_look_like_datetime(date_string):
--> 293         raise ValueError('Given date string not likely a datetime.')
    294 
    295     parsed, reso = _parse_delimited_date(date_string, dayfirst)

ValueError: Given date string not likely a datetime.

but the output should be Series([False, False]). The other comparison operators <, <=, >, and >= yield the same error, which also seems incorrect (the error message should probably be different, and the error should be a TypeError rather than a ValueError). This is happening on 0.25.1 and master.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions