Skip to content

Commit b6ffd89

Browse files
committed
ERR: more informative message on invalid Timestamp input
TST: fix unordable error message xref #14679 TST: handle unorderable exceptions in indexing closes #14684
1 parent bec5bdb commit b6ffd89

File tree

7 files changed

+46
-8
lines changed

7 files changed

+46
-8
lines changed

doc/source/whatsnew/v0.19.2.txt

+6
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,11 @@ Bug Fixes
4141
- Bug in not propogating exceptions in parsing invalid datetimes, noted in python 3.6 (:issue:`14561`)
4242

4343

44+
- Compat with python 3.6 for pickling of some offsets (:issue:`14685`)
45+
- Compat with python 3.6 for some indexing exception types (:issue:`14684`)
46+
- Compat with python 3.6 for deprecation warnings in the test suite (:issue:`14681`)
47+
48+
49+
4450

4551
- Explicit check in ``to_stata`` and ``StataWriter`` for out-of-range values when writing doubles (:issue:`14618`)

pandas/compat/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
PY2 = sys.version_info[0] == 2
4242
PY3 = (sys.version_info[0] >= 3)
4343
PY35 = (sys.version_info >= (3, 5))
44+
PY36 = (sys.version_info >= (3, 6))
4445

4546
try:
4647
import __builtin__ as builtins

pandas/core/indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
is_sequence,
1212
is_scalar,
1313
is_sparse,
14+
_is_unorderable_exception,
1415
_ensure_platform_int)
1516
from pandas.types.missing import isnull, _infer_fill_value
1617

@@ -1411,7 +1412,7 @@ def error():
14111412
except TypeError as e:
14121413

14131414
# python 3 type errors should be raised
1414-
if 'unorderable' in str(e): # pragma: no cover
1415+
if _is_unorderable_exception(e):
14151416
error()
14161417
raise
14171418
except:

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
is_iterator,
2626
is_dict_like,
2727
is_scalar,
28+
_is_unorderable_exception,
2829
_ensure_platform_int)
2930
from pandas.types.generic import ABCSparseArray, ABCDataFrame
3031
from pandas.types.cast import (_maybe_upcast, _infer_dtype_from_scalar,
@@ -753,7 +754,7 @@ def setitem(key, value):
753754
raise ValueError("Can only tuple-index with a MultiIndex")
754755

755756
# python 3 type errors should be raised
756-
if 'unorderable' in str(e): # pragma: no cover
757+
if _is_unorderable_exception(e):
757758
raise IndexError(key)
758759

759760
if com.is_bool_indexer(key):

pandas/tests/indexes/test_base.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .common import Base
88

99
from pandas.compat import (is_platform_windows, range, lrange, lzip, u,
10-
zip, PY3)
10+
zip, PY3, PY36)
1111
import operator
1212
import os
1313

@@ -1774,7 +1774,12 @@ def create_index(self):
17741774
def test_order(self):
17751775
idx = self.create_index()
17761776
# 9816 deprecated
1777-
if PY3:
1777+
if PY36:
1778+
with tm.assertRaisesRegexp(TypeError, "'>' not supported "
1779+
"between instances of 'str' and 'int'"):
1780+
with tm.assert_produces_warning(FutureWarning):
1781+
idx.order()
1782+
elif PY3:
17781783
with tm.assertRaisesRegexp(TypeError, "unorderable types"):
17791784
with tm.assert_produces_warning(FutureWarning):
17801785
idx.order()
@@ -1784,7 +1789,11 @@ def test_order(self):
17841789

17851790
def test_argsort(self):
17861791
idx = self.create_index()
1787-
if PY3:
1792+
if PY36:
1793+
with tm.assertRaisesRegexp(TypeError, "'>' not supported "
1794+
"between instances of 'str' and 'int'"):
1795+
result = idx.argsort()
1796+
elif PY3:
17881797
with tm.assertRaisesRegexp(TypeError, "unorderable types"):
17891798
result = idx.argsort()
17901799
else:
@@ -1794,7 +1803,11 @@ def test_argsort(self):
17941803

17951804
def test_numpy_argsort(self):
17961805
idx = self.create_index()
1797-
if PY3:
1806+
if PY36:
1807+
with tm.assertRaisesRegexp(TypeError, "'>' not supported "
1808+
"between instances of 'str' and 'int'"):
1809+
result = np.argsort(idx)
1810+
elif PY3:
17981811
with tm.assertRaisesRegexp(TypeError, "unorderable types"):
17991812
result = np.argsort(idx)
18001813
else:

pandas/tslib.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,8 @@ cdef convert_to_tsobject(object ts, object tz, object unit,
15391539
"Cannot convert Period to Timestamp "
15401540
"unambiguously. Use to_timestamp")
15411541
else:
1542-
raise TypeError('Cannot convert input to Timestamp')
1542+
raise TypeError('Cannot convert input [{}] of type {} to '
1543+
'Timestamp'.format(ts, type(ts)))
15431544

15441545
if obj.value != NPY_NAT:
15451546
_check_dts_bounds(&obj.dts)

pandas/types/common.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
""" common type operations """
22

33
import numpy as np
4-
from pandas.compat import string_types, text_type, binary_type
4+
from pandas.compat import (string_types, text_type, binary_type,
5+
PY3, PY36)
56
from pandas import lib, algos
67
from .dtypes import (CategoricalDtype, CategoricalDtypeType,
78
DatetimeTZDtype, DatetimeTZDtypeType,
@@ -188,6 +189,20 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
188189
return issubclass(tipo, (np.datetime64, np.timedelta64))
189190

190191

192+
def _is_unorderable_exception(e):
193+
"""
194+
return a boolean if we an unorderable exception error message
195+
196+
These are different error message for PY>=3<=3.5 and PY>=3.6
197+
"""
198+
if PY36:
199+
return ("'>' not supported between instances "
200+
"of 'str' and 'int'" in str(e))
201+
elif PY3:
202+
return 'unorderable' in str(e)
203+
return False
204+
205+
191206
def is_numeric_v_string_like(a, b):
192207
"""
193208
numpy doesn't like to compare numeric arrays vs scalar string-likes

0 commit comments

Comments
 (0)