Skip to content

BUG: Index of an empty Series cannot be compared to a Timestamp anymore #36983

Closed
@gabicca

Description

@gabicca
  • [Y] I have checked that this issue has not already been reported.

  • [Y] I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

def test_series_timestamp_comparison():
    my_series = pd.Series([],  dtype=np.float64)
    date = pd.Timestamp('2015-07-15 00:00:00')

    res = my_series.index < date
    assert res.dtype == bool
    assert len(res) == 0

Problem description

In pandas 1.1.2 the above test passes, while in 1.1.3 it throws an error on line 5:

Error in 1.1.3:
TypeError: '<' not supported between instances of 'numpy.ndarray' and 'Timestamp'

As far as I can tell, this started to occur because of a change in pandas.core.indexes.base._make_comparison_op,
which change was added by this PR: #36440

My question is if the new behaviour is legit for the above comparison, is it expected?

In 1.1.2 this comparison didn't fail, because it would end up in

        else:
            with np.errstate(all="ignore"):
                result = op(self._values, np.asarray(other))

which was changed to:

        elif is_interval_dtype(self.dtype):
            with np.errstate(all="ignore"):
                result = op(self._values, np.asarray(other))

and the else part started to use ops.comparison_op which now fails in the above example (since is_interval_dtype(self.dtype) is false for the above case).

A bit more background: the above comparison is used when subselecting data in the series without knowing whether the input series has data in it or not. So e.g.:

my_series[my_series.index < date] = 0.4

Apologies if this question has been raised, I couldn't find anything similar in Open Issues.

Output of pd.show_versions()

python : 3.7.4.final.0
pandas : 1.1.3
numpy : 1.19.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions