Skip to content

Commit f593426

Browse files
author
Seth Padowitz
committed
Merge remote-tracking branch 'upstream/master'
2 parents 3adc7b2 + 1a6d483 commit f593426

20 files changed

+201
-279
lines changed

doc/source/v0.14.1.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,18 @@ Bug Fixes
242242
- Bug in ``DatetimeIndex.to_period``, ``PeriodIndex.asobject``, ``PeriodIndex.to_timestamp`` doesn't preserve ``name`` (:issue:`7485`)
243243
- Bug in ``DatetimeIndex.to_period`` and ``PeriodIndex.to_timestanp`` handle ``NaT`` incorrectly (:issue:`7228`)
244244

245+
- BUG in ``offsets.apply``, ''rollforward`` and ``rollback`` may return normal ``datetime`` (:issue:`7502`)
245246

246247

247248
- BUG in ``resample`` raises ``ValueError`` when target contains ``NaT`` (:issue:`7227`)
248249

249250
- Bug in ``Timestamp.tz_localize`` resets ``nanosecond`` info (:issue:`7534`)
250251

252+
253+
251254
- Bug in ``Index.astype(float)`` where it would return an ``object`` dtype
252255
``Index`` (:issue:`7464`).
253-
256+
- Bug in ``DataFrame.reset_index`` loses ``tz`` (:issue:`3950`)
254257

255258

256259
- Bug in non-monotonic ``Index.union`` may preserve ``name`` incorrectly (:issue:`7458`)

pandas/core/frame.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,19 +2326,24 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0,
23262326
else:
23272327
new_obj = self.copy()
23282328

2329-
def _maybe_cast(values, labels=None):
2330-
2331-
if values.dtype == np.object_:
2332-
values = lib.maybe_convert_objects(values)
2333-
2334-
# if we have the labels, extract the values with a mask
2335-
if labels is not None:
2336-
mask = labels == -1
2337-
values = values.take(labels)
2338-
if mask.any():
2339-
values, changed = com._maybe_upcast_putmask(
2340-
values, mask, np.nan)
2341-
2329+
def _maybe_casted_values(index, labels=None):
2330+
if isinstance(index, PeriodIndex):
2331+
values = index.asobject
2332+
elif (isinstance(index, DatetimeIndex) and
2333+
index.tz is not None):
2334+
values = index.asobject
2335+
else:
2336+
values = index.values
2337+
if values.dtype == np.object_:
2338+
values = lib.maybe_convert_objects(values)
2339+
2340+
# if we have the labels, extract the values with a mask
2341+
if labels is not None:
2342+
mask = labels == -1
2343+
values = values.take(labels)
2344+
if mask.any():
2345+
values, changed = com._maybe_upcast_putmask(values,
2346+
mask, np.nan)
23422347
return values
23432348

23442349
new_index = np.arange(len(new_obj))
@@ -2371,7 +2376,7 @@ def _maybe_cast(values, labels=None):
23712376
col_name = tuple(name_lst)
23722377

23732378
# to ndarray and maybe infer different dtype
2374-
level_values = _maybe_cast(lev.values, lab)
2379+
level_values = _maybe_casted_values(lev, lab)
23752380
if level is None or i in level:
23762381
new_obj.insert(0, col_name, level_values)
23772382

@@ -2387,13 +2392,7 @@ def _maybe_cast(values, labels=None):
23872392
lev_num = self.columns._get_level_number(col_level)
23882393
name_lst[lev_num] = name
23892394
name = tuple(name_lst)
2390-
if isinstance(self.index, PeriodIndex):
2391-
values = self.index.asobject
2392-
elif (isinstance(self.index, DatetimeIndex) and
2393-
self.index.tz is not None):
2394-
values = self.index.asobject
2395-
else:
2396-
values = _maybe_cast(self.index.values)
2395+
values = _maybe_casted_values(self.index)
23972396
new_obj.insert(0, name, values)
23982397

23992398
new_obj.index = new_index

pandas/stats/tests/test_moments.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717

1818
N, K = 100, 10
1919

20-
def _skip_if_no_scipy():
21-
try:
22-
import scipy.stats
23-
except ImportError:
24-
raise nose.SkipTest("no scipy.stats")
2520

2621
class TestMoments(tm.TestCase):
2722

@@ -68,7 +63,7 @@ def test_rolling_mean(self):
6863
self._check_moment_func(mom.rolling_mean, np.mean)
6964

7065
def test_cmov_mean(self):
71-
_skip_if_no_scipy()
66+
tm._skip_if_no_scipy()
7267
try:
7368
from scikits.timeseries.lib import cmov_mean
7469
except ImportError:
@@ -86,7 +81,7 @@ def test_cmov_mean(self):
8681
assert_series_equal(xp, rs)
8782

8883
def test_cmov_window(self):
89-
_skip_if_no_scipy()
84+
tm._skip_if_no_scipy()
9085
try:
9186
from scikits.timeseries.lib import cmov_window
9287
except ImportError:
@@ -104,7 +99,7 @@ def test_cmov_window(self):
10499
assert_series_equal(xp, rs)
105100

106101
def test_cmov_window_corner(self):
107-
_skip_if_no_scipy()
102+
tm._skip_if_no_scipy()
108103
try:
109104
from scikits.timeseries.lib import cmov_window
110105
except ImportError:
@@ -128,7 +123,7 @@ def test_cmov_window_corner(self):
128123
self.assertEqual(len(rs), 5)
129124

130125
def test_cmov_window_frame(self):
131-
_skip_if_no_scipy()
126+
tm._skip_if_no_scipy()
132127
try:
133128
from scikits.timeseries.lib import cmov_window
134129
except ImportError:
@@ -141,7 +136,7 @@ def test_cmov_window_frame(self):
141136
assert_frame_equal(DataFrame(xp), rs)
142137

143138
def test_cmov_window_na_min_periods(self):
144-
_skip_if_no_scipy()
139+
tm._skip_if_no_scipy()
145140
try:
146141
from scikits.timeseries.lib import cmov_window
147142
except ImportError:
@@ -158,7 +153,7 @@ def test_cmov_window_na_min_periods(self):
158153
assert_series_equal(xp, rs)
159154

160155
def test_cmov_window_regular(self):
161-
_skip_if_no_scipy()
156+
tm._skip_if_no_scipy()
162157
try:
163158
from scikits.timeseries.lib import cmov_window
164159
except ImportError:
@@ -174,7 +169,7 @@ def test_cmov_window_regular(self):
174169
assert_series_equal(Series(xp), rs)
175170

176171
def test_cmov_window_special(self):
177-
_skip_if_no_scipy()
172+
tm._skip_if_no_scipy()
178173
try:
179174
from scikits.timeseries.lib import cmov_window
180175
except ImportError:

pandas/tests/test_format.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,6 @@ def skip_if_np_version_under1p7():
8686

8787
raise nose.SkipTest('numpy >= 1.7 required')
8888

89-
def _skip_if_no_pytz():
90-
try:
91-
import pytz
92-
except ImportError:
93-
raise nose.SkipTest("pytz not installed")
94-
95-
def _skip_if_no_dateutil():
96-
try:
97-
import dateutil
98-
except ImportError:
99-
raise nose.SkipTest("dateutil not installed")
100-
10189

10290
class TestDataFrameFormatting(tm.TestCase):
10391
_multiprocess_can_split_ = True
@@ -2930,7 +2918,7 @@ def test_no_tz(self):
29302918
self.assertEqual(str(ts_nanos_micros), "1970-01-01 00:00:00.000001200")
29312919

29322920
def test_tz_pytz(self):
2933-
_skip_if_no_pytz()
2921+
tm._skip_if_no_pytz()
29342922

29352923
import pytz
29362924

@@ -2944,7 +2932,7 @@ def test_tz_pytz(self):
29442932
self.assertEqual(str(dt_datetime_us), str(Timestamp(dt_datetime_us)))
29452933

29462934
def test_tz_dateutil(self):
2947-
_skip_if_no_dateutil()
2935+
tm._skip_if_no_dateutil()
29482936
import dateutil
29492937
utc = dateutil.tz.tzutc()
29502938

pandas/tests/test_frame.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@
5252

5353
from numpy.testing.decorators import slow
5454

55-
def _skip_if_no_scipy():
56-
try:
57-
import scipy.stats
58-
except ImportError:
59-
raise nose.SkipTest("no scipy.stats module")
60-
6155
#---------------------------------------------------------------------
6256
# DataFrame test cases
6357

@@ -2182,6 +2176,20 @@ def test_set_index_cast_datetimeindex(self):
21822176
df.pop('ts')
21832177
assert_frame_equal(df, expected)
21842178

2179+
# GH 3950
2180+
# reset_index with single level
2181+
for tz in ['UTC', 'Asia/Tokyo', 'US/Eastern']:
2182+
idx = pd.date_range('1/1/2011', periods=5, freq='D', tz=tz, name='idx')
2183+
df = pd.DataFrame({'a': range(5), 'b': ['A', 'B', 'C', 'D', 'E']}, index=idx)
2184+
2185+
expected = pd.DataFrame({'idx': [datetime(2011, 1, 1), datetime(2011, 1, 2),
2186+
datetime(2011, 1, 3), datetime(2011, 1, 4),
2187+
datetime(2011, 1, 5)],
2188+
'a': range(5), 'b': ['A', 'B', 'C', 'D', 'E']},
2189+
columns=['idx', 'a', 'b'])
2190+
expected['idx'] = expected['idx'].apply(lambda d: pd.Timestamp(d, tz=tz))
2191+
assert_frame_equal(df.reset_index(), expected)
2192+
21852193
def test_set_index_multiindexcolumns(self):
21862194
columns = MultiIndex.from_tuples([('foo', 1), ('foo', 2), ('bar', 1)])
21872195
df = DataFrame(np.random.randn(3, 3), columns=columns)
@@ -6739,28 +6747,28 @@ def _check_method(self, method='pearson', check_minp=False):
67396747
expected.ix['A', 'B'] = expected.ix['B', 'A'] = nan
67406748

67416749
def test_corr_pearson(self):
6742-
_skip_if_no_scipy()
6750+
tm._skip_if_no_scipy()
67436751
self.frame['A'][:5] = nan
67446752
self.frame['B'][5:10] = nan
67456753

67466754
self._check_method('pearson')
67476755

67486756
def test_corr_kendall(self):
6749-
_skip_if_no_scipy()
6757+
tm._skip_if_no_scipy()
67506758
self.frame['A'][:5] = nan
67516759
self.frame['B'][5:10] = nan
67526760

67536761
self._check_method('kendall')
67546762

67556763
def test_corr_spearman(self):
6756-
_skip_if_no_scipy()
6764+
tm._skip_if_no_scipy()
67576765
self.frame['A'][:5] = nan
67586766
self.frame['B'][5:10] = nan
67596767

67606768
self._check_method('spearman')
67616769

67626770
def test_corr_non_numeric(self):
6763-
_skip_if_no_scipy()
6771+
tm._skip_if_no_scipy()
67646772
self.frame['A'][:5] = nan
67656773
self.frame['B'][5:10] = nan
67666774

@@ -6770,7 +6778,7 @@ def test_corr_non_numeric(self):
67706778
assert_frame_equal(result, expected)
67716779

67726780
def test_corr_nooverlap(self):
6773-
_skip_if_no_scipy()
6781+
tm._skip_if_no_scipy()
67746782

67756783
# nothing in common
67766784
for meth in ['pearson', 'kendall', 'spearman']:
@@ -6783,7 +6791,7 @@ def test_corr_nooverlap(self):
67836791
self.assertEqual(rs.ix['B', 'B'], 1)
67846792

67856793
def test_corr_constant(self):
6786-
_skip_if_no_scipy()
6794+
tm._skip_if_no_scipy()
67876795

67886796
# constant --> all NA
67896797

@@ -10957,7 +10965,7 @@ def test_sem(self):
1095710965
nanops._USE_BOTTLENECK = True
1095810966

1095910967
def test_skew(self):
10960-
_skip_if_no_scipy()
10968+
tm._skip_if_no_scipy()
1096110969
from scipy.stats import skew
1096210970

1096310971
def alt(x):
@@ -10968,7 +10976,7 @@ def alt(x):
1096810976
self._check_stat_op('skew', alt)
1096910977

1097010978
def test_kurt(self):
10971-
_skip_if_no_scipy()
10979+
tm._skip_if_no_scipy()
1097210980

1097310981
from scipy.stats import kurtosis
1097410982

@@ -11320,7 +11328,7 @@ def test_cumprod(self):
1132011328
df.cumprod(1)
1132111329

1132211330
def test_rank(self):
11323-
_skip_if_no_scipy()
11331+
tm._skip_if_no_scipy()
1132411332
from scipy.stats import rankdata
1132511333

1132611334
self.frame['A'][::2] = np.nan
@@ -11412,7 +11420,7 @@ def test_rank2(self):
1141211420
assert_frame_equal(df.rank(), exp)
1141311421

1141411422
def test_rank_na_option(self):
11415-
_skip_if_no_scipy()
11423+
tm._skip_if_no_scipy()
1141611424
from scipy.stats import rankdata
1141711425

1141811426
self.frame['A'][::2] = np.nan

0 commit comments

Comments
 (0)