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()
Comment From: jreback
there are some issues about this already see if u can find them
Comment From: jbrockmendel
There are a few that are broadly related but I didn't see any about dt64/td64 dtypes. AFAICT the issue that needs to be nailed down before I can fix is: what are these operations supposed to represent for these dtypes? Since bitwise ops don't make a lot of sense for these dtypes it may just be a matter of making Series.astype(bool)
work for datetime64 and timedelta64 dtypes (or ideally an implementation with less far-reaching effects)
Comment From: mroeschke
FWIW the TDI/Series case returns a value now:
``` In [73]: tdi = pd.TimedeltaIndex(['1 Day', '2 Days', 'NaT']) ...: ser = pd.Series(tdi)
In [74]: ser & tdi Out[74]: 0 False 1 False 2 False dtype: bool```
Comment From: jbrockmendel
I would have expected [True, True, False]; wouldn't you?
Comment From: mroeschke
Agreed. Just noting that this doesn't raise a ValueError
anymore
Comment From: jorisvandenbossche
An option is also to simply raise an error stating that logical ops are not supported for timedelta/datetime dtypes ?
Comment From: jbrockmendel
These all now raise in ways that make sense to me. Closing.