Skip to content

Series[datetime64/timedelta64] bool ops broken #19759

Closed
@jbrockmendel

Description

@jbrockmendel
dti = pd.date_range('2016-01-01', periods=3)
ser = pd.Series(dti)
tr = np.array([True, True, True])

>>> ser & tr
0    False
1    False
2    False
dtype: bool

>>> ser | ser
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/ops.py", line 1047, in wrapper
    other = fill_int(other) if is_other_int_dtype else fill_bool(other)
  File "pandas/core/ops.py", line 1036, in <lambda>
    fill_bool = lambda x: x.fillna(False).astype(bool)
  File "pandas/util/_decorators.py", line 138, in wrapper
    return func(*args, **kwargs)
  File "pandas/core/generic.py", line 4443, in astype
    **kwargs)
  File "pandas/core/internals.py", line 3606, in astype
    return self.apply('astype', dtype=dtype, **kwargs)
  File "pandas/core/internals.py", line 3473, in apply
    applied = getattr(b, f)(**kwargs)
  File "pandas/core/internals.py", line 569, in astype
    **kwargs)
  File "pandas/core/internals.py", line 2603, in _astype
    return super(DatetimeBlock, self)._astype(dtype=dtype, **kwargs)
  File "pandas/core/internals.py", line 657, in _astype
    values = astype_nansafe(values.ravel(), dtype, copy=True)
  File "pandas/core/dtypes/cast.py", line 668, in astype_nansafe
    to_dtype=dtype))
TypeError: cannot astype a datetimelike from [datetime64[ns]] to [bool]

It isn't clear to me what the desired behavior is here.

Also operating with an Index falls through to a case that I think is intended to only catch scalars:

tdi = pd.TimedeltaIndex(['1 Day', '2 Days', 'NaT'])
ser = pd.Series(tdi)
>>> ser & tdi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/ops.py", line 1062, in wrapper
    res_values = na_op(self.values, other)
  File "pandas/core/ops.py", line 1020, in na_op
    if not isna(y):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDatetimeDatetime data dtypeNumeric OperationsArithmetic, Comparison, and Logical operationsTimedeltaTimedelta data type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions