Skip to content

Commit dc2a5b3

Browse files
committed
BUG: to_numeric should raise if input is more than one dimension pandas-dev#11776
1 parent 5b84e49 commit dc2a5b3

File tree

7 files changed

+51
-18
lines changed

7 files changed

+51
-18
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,4 @@ Bug Fixes
196196
- Bug in ``pd.rolling_median`` where memory allocation failed even with sufficient memory (:issue:`11696`)
197197

198198
- Bug in ``df.replace`` while replacing value in mixed dtype ``Dataframe`` (:issue:`11698`)
199+
- Bug in ``to_numeric`` where it does not raise if input is more than one dimension (:issue:`11776`)

pandas/tools/tests/test_util.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ def test_error(self):
113113
expected = pd.Series([1, -3.14, np.nan])
114114
tm.assert_series_equal(res, expected)
115115

116-
117116
def test_list(self):
118117
s = ['1', '-3.14', '7']
119118
res = to_numeric(s)
@@ -136,6 +135,14 @@ def test_all_nan(self):
136135
expected = pd.Series([np.nan, np.nan, np.nan])
137136
tm.assert_series_equal(res, expected)
138137

138+
def test_type_check(self):
139+
# GH 11776
140+
df = pd.DataFrame({'a': [1, -3.14, 7], 'b': ['4', '5', '6']})
141+
with tm.assertRaisesRegexp(TypeError, "1-d array"):
142+
to_numeric(df)
143+
for errors in ['ignore', 'raise', 'coerce']:
144+
with tm.assertRaisesRegexp(TypeError, "1-d array"):
145+
to_numeric(df, errors=errors)
139146

140147
if __name__ == '__main__':
141148
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],

pandas/tools/util.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def to_numeric(arg, errors='raise'):
5656
5757
Parameters
5858
----------
59-
arg : list, tuple or array of objects, or Series
59+
arg : list, tuple, 1-d array, or Series
6060
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
6161
- If 'raise', then invalid parsing will raise an exception
6262
- If 'coerce', then invalid parsing will be set as NaN
@@ -84,6 +84,8 @@ def to_numeric(arg, errors='raise'):
8484
index, name = arg.index, arg.name
8585
elif isinstance(arg, (list, tuple)):
8686
arg = np.array(arg, dtype='O')
87+
elif getattr(arg, 'ndim', 1) > 1:
88+
raise TypeError('arg must be a list, tuple, 1-d array, or Series')
8789

8890
conv = arg
8991
arg = com._ensure_object(arg)

pandas/tseries/tests/test_period.py

+10
Original file line numberDiff line numberDiff line change
@@ -2984,6 +2984,16 @@ def test_to_datetime_1703(self):
29842984
result = index.to_datetime()
29852985
self.assertEqual(result[0], Timestamp('1/1/2012'))
29862986

2987+
def test_to_datetime_dimensions(self):
2988+
# GH 11776
2989+
df = DataFrame({'a': ['1/1/2012', '1/2/2012'],
2990+
'b': ['12/30/2012', '12/31/2012']})
2991+
with tm.assertRaisesRegexp(TypeError, "1-d array"):
2992+
to_datetime(df)
2993+
for errors in ['ignore', 'raise', 'coerce']:
2994+
with tm.assertRaisesRegexp(TypeError, "1-d array"):
2995+
to_datetime(df, errors=errors)
2996+
29872997
def test_get_loc_msg(self):
29882998
idx = period_range('2000-1-1', freq='A', periods=10)
29892999
bad_period = Period('2012', 'A')

pandas/tseries/tests/test_timedeltas.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -444,26 +444,36 @@ def check(value):
444444

445445
def test_timedelta_range(self):
446446

447-
expected = to_timedelta(np.arange(5),unit='D')
448-
result = timedelta_range('0 days',periods=5,freq='D')
447+
expected = to_timedelta(np.arange(5), unit='D')
448+
result = timedelta_range('0 days', periods=5, freq='D')
449449
tm.assert_index_equal(result, expected)
450450

451-
expected = to_timedelta(np.arange(11),unit='D')
452-
result = timedelta_range('0 days','10 days',freq='D')
451+
expected = to_timedelta(np.arange(11), unit='D')
452+
result = timedelta_range('0 days', '10 days', freq='D')
453453
tm.assert_index_equal(result, expected)
454454

455-
expected = to_timedelta(np.arange(5),unit='D') + Second(2) + Day()
456-
result = timedelta_range('1 days, 00:00:02','5 days, 00:00:02',freq='D')
455+
expected = to_timedelta(np.arange(5), unit='D') + Second(2) + Day()
456+
result = timedelta_range('1 days, 00:00:02', '5 days, 00:00:02', freq='D')
457457
tm.assert_index_equal(result, expected)
458458

459-
expected = to_timedelta([1,3,5,7,9],unit='D') + Second(2)
460-
result = timedelta_range('1 days, 00:00:02',periods=5,freq='2D')
459+
expected = to_timedelta([1,3,5,7,9], unit='D') + Second(2)
460+
result = timedelta_range('1 days, 00:00:02', periods=5, freq='2D')
461461
tm.assert_index_equal(result, expected)
462462

463-
expected = to_timedelta(np.arange(50),unit='T')*30
464-
result = timedelta_range('0 days',freq='30T',periods=50)
463+
expected = to_timedelta(np.arange(50), unit='T') * 30
464+
result = timedelta_range('0 days', freq='30T', periods=50)
465465
tm.assert_index_equal(result, expected)
466466

467+
# GH 11776
468+
arr = np.arange(10).reshape(2, 5)
469+
df = pd.DataFrame(np.arange(10).reshape(2, 5))
470+
for arg in (arr, df):
471+
with tm.assertRaisesRegexp(TypeError, "1-d array"):
472+
to_timedelta(arg)
473+
for errors in ['ignore', 'raise', 'coerce']:
474+
with tm.assertRaisesRegexp(TypeError, "1-d array"):
475+
to_timedelta(arg, errors=errors)
476+
467477
# issue10583
468478
df = pd.DataFrame(np.random.normal(size=(10,4)))
469479
df.index = pd.timedelta_range(start='0s', periods=10, freq='s')

pandas/tseries/timedeltas.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise', coerce=None):
1919
2020
Parameters
2121
----------
22-
arg : string, timedelta, array of strings (with possible NAs)
22+
arg : string, timedelta, list, tuple, 1-d array, or Series
2323
unit : unit of the arg (D,h,m,s,ms,us,ns) denote the unit, which is an integer/float number
2424
box : boolean, default True
2525
- If True returns a Timedelta/TimedeltaIndex of the results
@@ -37,7 +37,7 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise', coerce=None):
3737

3838
def _convert_listlike(arg, box, unit, name=None):
3939

40-
if isinstance(arg, (list,tuple)) or ((hasattr(arg,'__iter__') and not hasattr(arg,'dtype'))):
40+
if isinstance(arg, (list, tuple)) or not hasattr(arg, 'dtype'):
4141
arg = np.array(list(arg), dtype='O')
4242

4343
# these are shortcutable
@@ -62,8 +62,10 @@ def _convert_listlike(arg, box, unit, name=None):
6262
return Series(values, index=arg.index, name=arg.name, dtype='m8[ns]')
6363
elif isinstance(arg, ABCIndexClass):
6464
return _convert_listlike(arg, box=box, unit=unit, name=arg.name)
65-
elif is_list_like(arg):
65+
elif is_list_like(arg) and getattr(arg, 'ndim', 1) == 1:
6666
return _convert_listlike(arg, box=box, unit=unit)
67+
elif getattr(arg, 'ndim', 1) > 1:
68+
raise TypeError('arg must be a string, timedelta, list, tuple, 1-d array, or Series')
6769

6870
# ...so it must be a scalar value. Return scalar.
6971
return _coerce_scalar_to_timedelta_type(arg, unit=unit, box=box, errors=errors)

pandas/tseries/tools.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
188188
189189
Parameters
190190
----------
191-
arg : string, datetime, array of strings (with possible NAs)
191+
arg : string, datetime, list, tuple, 1-d array, or Series
192192
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
193193
- If 'raise', then invalid parsing will raise an exception
194194
- If 'coerce', then invalid parsing will be set as NaT
@@ -288,7 +288,7 @@ def _to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
288288

289289
def _convert_listlike(arg, box, format, name=None):
290290

291-
if isinstance(arg, (list,tuple)):
291+
if isinstance(arg, (list, tuple)):
292292
arg = np.array(arg, dtype='O')
293293

294294
# these are shortcutable
@@ -312,8 +312,9 @@ def _convert_listlike(arg, box, format, name=None):
312312
result = arg.astype('datetime64[ns]')
313313
if box:
314314
return DatetimeIndex(result, tz='utc' if utc else None, name=name)
315-
316315
return result
316+
elif getattr(arg, 'ndim', 1) > 1:
317+
raise TypeError('arg must be a string, datetime, list, tuple, 1-d array, or Series')
317318

318319
arg = com._ensure_object(arg)
319320
require_iso8601 = False

0 commit comments

Comments
 (0)