Skip to content

Commit 488db6f

Browse files
authored
TST: add back test_generic.py, accid removed in the big reorg (#18114)
1 parent a2d0eed commit 488db6f

File tree

10 files changed

+1733
-23
lines changed

10 files changed

+1733
-23
lines changed

pandas/core/generic.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def _get_axis_number(self, axis):
352352
else:
353353
try:
354354
return self._AXIS_NUMBERS[axis]
355-
except:
355+
except KeyError:
356356
pass
357357
raise ValueError('No axis named {0} for object type {1}'
358358
.format(axis, type(self)))
@@ -365,7 +365,7 @@ def _get_axis_name(self, axis):
365365
else:
366366
try:
367367
return self._AXIS_NAMES[axis]
368-
except:
368+
except KeyError:
369369
pass
370370
raise ValueError('No axis named {0} for object type {1}'
371371
.format(axis, type(self)))
@@ -701,7 +701,7 @@ def squeeze(self, axis=None):
701701
return self.iloc[
702702
tuple([0 if i in axis and len(a) == 1 else slice(None)
703703
for i, a in enumerate(self.axes)])]
704-
except:
704+
except Exception:
705705
return self
706706

707707
def swaplevel(self, i=-2, j=-1, axis=0):
@@ -1021,7 +1021,7 @@ def __invert__(self):
10211021
try:
10221022
arr = operator.inv(_values_from_object(self))
10231023
return self.__array_wrap__(arr)
1024-
except:
1024+
except Exception:
10251025

10261026
# inv fails with 0 len
10271027
if not np.prod(self.shape):
@@ -1907,7 +1907,7 @@ def _maybe_update_cacher(self, clear=False, verify_is_copy=True):
19071907
else:
19081908
try:
19091909
ref._maybe_cache_changed(cacher[0], self)
1910-
except:
1910+
except Exception:
19111911
pass
19121912

19131913
if verify_is_copy:
@@ -2016,15 +2016,15 @@ def _check_setitem_copy(self, stacklevel=4, t='setting', force=False):
20162016
if not gc.get_referents(self.is_copy()):
20172017
self.is_copy = None
20182018
return
2019-
except:
2019+
except Exception:
20202020
pass
20212021

20222022
# we might be a false positive
20232023
try:
20242024
if self.is_copy().shape == self.shape:
20252025
self.is_copy = None
20262026
return
2027-
except:
2027+
except Exception:
20282028
pass
20292029

20302030
# a custom message
@@ -2999,7 +2999,7 @@ def reindex(self, *args, **kwargs):
29992999
if self._needs_reindex_multi(axes, method, level):
30003000
try:
30013001
return self._reindex_multi(axes, copy, fill_value)
3002-
except:
3002+
except Exception:
30033003
pass
30043004

30053005
# perform the reindex on the axes
@@ -3715,7 +3715,7 @@ def _check_inplace_setting(self, value):
37153715
try:
37163716
if np.isnan(value):
37173717
return True
3718-
except:
3718+
except Exception:
37193719
pass
37203720

37213721
raise TypeError('Cannot do inplace boolean setting on '
@@ -5005,6 +5005,8 @@ def clip(self, lower=None, upper=None, axis=None, inplace=False,
50055005
inplace = validate_bool_kwarg(inplace, 'inplace')
50065006

50075007
axis = nv.validate_clip_with_axis(axis, args, kwargs)
5008+
if axis is not None:
5009+
axis = self._get_axis_number(axis)
50085010

50095011
# GH 17276
50105012
# numpy doesn't like NaN as a clip value
@@ -5916,7 +5918,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
59165918
new_other = _values_from_object(self).copy()
59175919
new_other[icond] = other
59185920
other = new_other
5919-
except:
5921+
except Exception:
59205922
try_quick = False
59215923

59225924
# let's create a new (if we failed at the above

pandas/tests/frame/test_analytics.py

+18
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,24 @@ def test_built_in_round(self):
18221822
{'col1': [1., 2., 3.], 'col2': [1., 2., 3.]})
18231823
tm.assert_frame_equal(round(df), expected_rounded)
18241824

1825+
def test_pct_change(self):
1826+
# GH 11150
1827+
pnl = DataFrame([np.arange(0, 40, 10), np.arange(0, 40, 10), np.arange(
1828+
0, 40, 10)]).astype(np.float64)
1829+
pnl.iat[1, 0] = np.nan
1830+
pnl.iat[1, 1] = np.nan
1831+
pnl.iat[2, 3] = 60
1832+
1833+
mask = pnl.isnull()
1834+
1835+
for axis in range(2):
1836+
expected = pnl.ffill(axis=axis) / pnl.ffill(axis=axis).shift(
1837+
axis=axis) - 1
1838+
expected[mask] = np.nan
1839+
result = pnl.pct_change(axis=axis, fill_method='pad')
1840+
1841+
tm.assert_frame_equal(result, expected)
1842+
18251843
# Clip
18261844

18271845
def test_clip(self):

pandas/tests/frame/test_timeseries.py

+79-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
import numpy as np
1212

1313
from pandas import (DataFrame, Series, Index,
14-
Timestamp, DatetimeIndex,
15-
to_datetime, date_range)
14+
Timestamp, DatetimeIndex, MultiIndex,
15+
to_datetime, date_range, period_range)
1616
import pandas as pd
1717
import pandas.tseries.offsets as offsets
1818

19-
from pandas.util.testing import assert_series_equal, assert_frame_equal
19+
from pandas.util.testing import (assert_series_equal,
20+
assert_frame_equal,
21+
assert_index_equal,
22+
assert_raises_regex)
2023

2124
import pandas.util.testing as tm
2225
from pandas.compat import product
@@ -601,3 +604,76 @@ def test_frame_to_period(self):
601604
tm.assert_index_equal(pts.columns, exp.columns.asfreq('M'))
602605

603606
pytest.raises(ValueError, df.to_period, axis=2)
607+
608+
@pytest.mark.parametrize("fn", ['tz_localize', 'tz_convert'])
609+
def test_tz_convert_and_localize(self, fn):
610+
l0 = date_range('20140701', periods=5, freq='D')
611+
612+
# TODO: l1 should be a PeriodIndex for testing
613+
# after GH2106 is addressed
614+
with pytest.raises(NotImplementedError):
615+
period_range('20140701', periods=1).tz_convert('UTC')
616+
with pytest.raises(NotImplementedError):
617+
period_range('20140701', periods=1).tz_localize('UTC')
618+
# l1 = period_range('20140701', periods=5, freq='D')
619+
l1 = date_range('20140701', periods=5, freq='D')
620+
621+
int_idx = Index(range(5))
622+
623+
if fn == 'tz_convert':
624+
l0 = l0.tz_localize('UTC')
625+
l1 = l1.tz_localize('UTC')
626+
627+
for idx in [l0, l1]:
628+
629+
l0_expected = getattr(idx, fn)('US/Pacific')
630+
l1_expected = getattr(idx, fn)('US/Pacific')
631+
632+
df1 = DataFrame(np.ones(5), index=l0)
633+
df1 = getattr(df1, fn)('US/Pacific')
634+
assert_index_equal(df1.index, l0_expected)
635+
636+
# MultiIndex
637+
# GH7846
638+
df2 = DataFrame(np.ones(5), MultiIndex.from_arrays([l0, l1]))
639+
640+
df3 = getattr(df2, fn)('US/Pacific', level=0)
641+
assert not df3.index.levels[0].equals(l0)
642+
assert_index_equal(df3.index.levels[0], l0_expected)
643+
assert_index_equal(df3.index.levels[1], l1)
644+
assert not df3.index.levels[1].equals(l1_expected)
645+
646+
df3 = getattr(df2, fn)('US/Pacific', level=1)
647+
assert_index_equal(df3.index.levels[0], l0)
648+
assert not df3.index.levels[0].equals(l0_expected)
649+
assert_index_equal(df3.index.levels[1], l1_expected)
650+
assert not df3.index.levels[1].equals(l1)
651+
652+
df4 = DataFrame(np.ones(5),
653+
MultiIndex.from_arrays([int_idx, l0]))
654+
655+
# TODO: untested
656+
df5 = getattr(df4, fn)('US/Pacific', level=1) # noqa
657+
658+
assert_index_equal(df3.index.levels[0], l0)
659+
assert not df3.index.levels[0].equals(l0_expected)
660+
assert_index_equal(df3.index.levels[1], l1_expected)
661+
assert not df3.index.levels[1].equals(l1)
662+
663+
# Bad Inputs
664+
665+
# Not DatetimeIndex / PeriodIndex
666+
with assert_raises_regex(TypeError, 'DatetimeIndex'):
667+
df = DataFrame(index=int_idx)
668+
df = getattr(df, fn)('US/Pacific')
669+
670+
# Not DatetimeIndex / PeriodIndex
671+
with assert_raises_regex(TypeError, 'DatetimeIndex'):
672+
df = DataFrame(np.ones(5),
673+
MultiIndex.from_arrays([int_idx, l0]))
674+
df = getattr(df, fn)('US/Pacific', level=0)
675+
676+
# Invalid level
677+
with assert_raises_regex(ValueError, 'not valid'):
678+
df = DataFrame(index=l0)
679+
df = getattr(df, fn)('US/Pacific', level=1)

pandas/tests/generic/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)