Skip to content

ERR: better exception for converting bool to datetime with 0.17 #11853

Closed
@kaotika

Description

@kaotika

While updating pandas-qt I stumbled about the following exception. Running the same with pandas 0.16 doesn't shows any exception. I think converting a bool to a datetime should raise some exception, but not the one shown. ;-)

import pandas as pd
import pytest

data = [
    ["text", 123, 12.3, True, "2015-12-16"]
]
df = pd.DataFrame(data, columns=["text", "int", "float", "bool", "date"])

with pytest.raises(ValueError) as err:
    df["text"].apply(pd.to_datetime)
assert 'Unknown string format' in str(err.value)

df["int"].apply(pd.to_datetime)
df["float"].apply(pd.to_datetime)
df["date"].apply(pd.to_datetime)
df["bool"].apply(pd.to_datetime)

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    df["bool"].apply(pd.to_datetime)
  File "/home/kaotika/.virtualenvs/pandas-qt-test/local/lib/python2.7/site-packages/pandas/core/series.py", line 2169, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/src/inference.pyx", line 1059, in pandas.lib.map_infer (pandas/lib.c:62578)
  File "/home/kaotika/.virtualenvs/pandas-qt-test/local/lib/python2.7/site-packages/pandas/util/decorators.py", line 89, in wrapper
    return func(*args, **kwargs)
  File "/home/kaotika/.virtualenvs/pandas-qt-test/local/lib/python2.7/site-packages/pandas/tseries/tools.py", line 276, in to_datetime
    unit=unit, infer_datetime_format=infer_datetime_format)
  File "/home/kaotika/.virtualenvs/pandas-qt-test/local/lib/python2.7/site-packages/pandas/tseries/tools.py", line 397, in _to_datetime
    return _convert_listlike(np.array([ arg ]), box, format)[0]
  File "/home/kaotika/.virtualenvs/pandas-qt-test/local/lib/python2.7/site-packages/pandas/tseries/tools.py", line 372, in _convert_listlike
    require_iso8601=require_iso8601)
  File "pandas/tslib.pyx", line 1847, in pandas.tslib.array_to_datetime (pandas/tslib.c:37155)
  File "pandas/tslib.pyx", line 2041, in pandas.tslib.array_to_datetime (pandas/tslib.c:36836)
  File "pandas/tslib.pyx", line 1986, in pandas.tslib.array_to_datetime (pandas/tslib.c:35842)
  File "pandas/tslib.pyx", line 1939, in pandas.tslib.array_to_datetime (pandas/tslib.c:34965)
TypeError: object of type 'bool' has no len()

Metadata

Metadata

Assignees

No one assigned

    Labels

    DatetimeDatetime data dtypeError ReportingIncorrect or improved errors from pandas

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions