Skip to content

Commit 1650142

Browse files
authored
Merge branch 'master' into fix_17415
2 parents cc6e7d6 + 265e327 commit 1650142

File tree

13 files changed

+257
-272
lines changed

13 files changed

+257
-272
lines changed

asv_bench/benchmarks/gil.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,35 @@ def setup(self, method):
180180
raise NotImplementedError
181181
win = 100
182182
arr = np.random.rand(100000)
183-
rolling = {'rolling_median': rolling_median,
184-
'rolling_mean': rolling_mean,
185-
'rolling_min': rolling_min,
186-
'rolling_max': rolling_max,
187-
'rolling_var': rolling_var,
188-
'rolling_skew': rolling_skew,
189-
'rolling_kurt': rolling_kurt,
190-
'rolling_std': rolling_std}
191-
192-
@test_parallel(num_threads=2)
193-
def parallel_rolling():
194-
rolling[method](arr, win)
195-
self.parallel_rolling = parallel_rolling
183+
if hasattr(DataFrame, 'rolling'):
184+
rolling = {'rolling_median': 'median',
185+
'rolling_mean': 'mean',
186+
'rolling_min': 'min',
187+
'rolling_max': 'max',
188+
'rolling_var': 'var',
189+
'rolling_skew': 'skew',
190+
'rolling_kurt': 'kurt',
191+
'rolling_std': 'std'}
192+
df = DataFrame(arr).rolling(win)
193+
194+
@test_parallel(num_threads=2)
195+
def parallel_rolling():
196+
getattr(df, rolling[method])()
197+
self.parallel_rolling = parallel_rolling
198+
else:
199+
rolling = {'rolling_median': rolling_median,
200+
'rolling_mean': rolling_mean,
201+
'rolling_min': rolling_min,
202+
'rolling_max': rolling_max,
203+
'rolling_var': rolling_var,
204+
'rolling_skew': rolling_skew,
205+
'rolling_kurt': rolling_kurt,
206+
'rolling_std': rolling_std}
207+
208+
@test_parallel(num_threads=2)
209+
def parallel_rolling():
210+
rolling[method](arr, win)
211+
self.parallel_rolling = parallel_rolling
196212

197213
def time_rolling(self, method):
198214
self.parallel_rolling()

asv_bench/benchmarks/rolling.py

Lines changed: 28 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,185 +1,41 @@
1-
from .pandas_vb_common import *
21
import pandas as pd
32
import numpy as np
43

4+
from .pandas_vb_common import setup # noqa
55

6-
class DataframeRolling(object):
7-
goal_time = 0.2
86

9-
def setup(self):
10-
self.N = 100000
11-
self.Ns = 10000
12-
self.df = pd.DataFrame({'a': np.random.random(self.N)})
13-
self.dfs = pd.DataFrame({'a': np.random.random(self.Ns)})
14-
self.wins = 10
15-
self.winl = 1000
7+
class Methods(object):
168

17-
def time_rolling_quantile_0(self):
18-
(self.df.rolling(self.wins).quantile(0.0))
9+
sample_time = 0.2
10+
params = (['DataFrame', 'Series'],
11+
[10, 1000],
12+
['int', 'float'],
13+
['median', 'mean', 'max', 'min', 'std', 'count', 'skew', 'kurt',
14+
'sum', 'corr', 'cov'])
15+
param_names = ['contructor', 'window', 'dtype', 'method']
1916

20-
def time_rolling_quantile_1(self):
21-
(self.df.rolling(self.wins).quantile(1.0))
17+
def setup(self, contructor, window, dtype, method):
18+
N = 10**5
19+
arr = np.random.random(N).astype(dtype)
20+
self.roll = getattr(pd, contructor)(arr).rolling(window)
2221

23-
def time_rolling_quantile_median(self):
24-
(self.df.rolling(self.wins).quantile(0.5))
22+
def time_rolling(self, contructor, window, dtype, method):
23+
getattr(self.roll, method)()
2524

26-
def time_rolling_median(self):
27-
(self.df.rolling(self.wins).median())
2825

29-
def time_rolling_mean(self):
30-
(self.df.rolling(self.wins).mean())
26+
class Quantile(object):
3127

32-
def time_rolling_max(self):
33-
(self.df.rolling(self.wins).max())
28+
sample_time = 0.2
29+
params = (['DataFrame', 'Series'],
30+
[10, 1000],
31+
['int', 'float'],
32+
[0, 0.5, 1])
33+
param_names = ['contructor', 'window', 'dtype', 'percentile']
3434

35-
def time_rolling_min(self):
36-
(self.df.rolling(self.wins).min())
35+
def setup(self, contructor, window, dtype, percentile):
36+
N = 10**5
37+
arr = np.random.random(N).astype(dtype)
38+
self.roll = getattr(pd, contructor)(arr).rolling(window)
3739

38-
def time_rolling_std(self):
39-
(self.df.rolling(self.wins).std())
40-
41-
def time_rolling_count(self):
42-
(self.df.rolling(self.wins).count())
43-
44-
def time_rolling_skew(self):
45-
(self.df.rolling(self.wins).skew())
46-
47-
def time_rolling_kurt(self):
48-
(self.df.rolling(self.wins).kurt())
49-
50-
def time_rolling_sum(self):
51-
(self.df.rolling(self.wins).sum())
52-
53-
def time_rolling_corr(self):
54-
(self.dfs.rolling(self.wins).corr())
55-
56-
def time_rolling_cov(self):
57-
(self.dfs.rolling(self.wins).cov())
58-
59-
def time_rolling_quantile_0_l(self):
60-
(self.df.rolling(self.winl).quantile(0.0))
61-
62-
def time_rolling_quantile_1_l(self):
63-
(self.df.rolling(self.winl).quantile(1.0))
64-
65-
def time_rolling_quantile_median_l(self):
66-
(self.df.rolling(self.winl).quantile(0.5))
67-
68-
def time_rolling_median_l(self):
69-
(self.df.rolling(self.winl).median())
70-
71-
def time_rolling_mean_l(self):
72-
(self.df.rolling(self.winl).mean())
73-
74-
def time_rolling_max_l(self):
75-
(self.df.rolling(self.winl).max())
76-
77-
def time_rolling_min_l(self):
78-
(self.df.rolling(self.winl).min())
79-
80-
def time_rolling_std_l(self):
81-
(self.df.rolling(self.wins).std())
82-
83-
def time_rolling_count_l(self):
84-
(self.df.rolling(self.wins).count())
85-
86-
def time_rolling_skew_l(self):
87-
(self.df.rolling(self.wins).skew())
88-
89-
def time_rolling_kurt_l(self):
90-
(self.df.rolling(self.wins).kurt())
91-
92-
def time_rolling_sum_l(self):
93-
(self.df.rolling(self.wins).sum())
94-
95-
96-
class SeriesRolling(object):
97-
goal_time = 0.2
98-
99-
def setup(self):
100-
self.N = 100000
101-
self.Ns = 10000
102-
self.df = pd.DataFrame({'a': np.random.random(self.N)})
103-
self.dfs = pd.DataFrame({'a': np.random.random(self.Ns)})
104-
self.sr = self.df.a
105-
self.srs = self.dfs.a
106-
self.wins = 10
107-
self.winl = 1000
108-
109-
def time_rolling_quantile_0(self):
110-
(self.sr.rolling(self.wins).quantile(0.0))
111-
112-
def time_rolling_quantile_1(self):
113-
(self.sr.rolling(self.wins).quantile(1.0))
114-
115-
def time_rolling_quantile_median(self):
116-
(self.sr.rolling(self.wins).quantile(0.5))
117-
118-
def time_rolling_median(self):
119-
(self.sr.rolling(self.wins).median())
120-
121-
def time_rolling_mean(self):
122-
(self.sr.rolling(self.wins).mean())
123-
124-
def time_rolling_max(self):
125-
(self.sr.rolling(self.wins).max())
126-
127-
def time_rolling_min(self):
128-
(self.sr.rolling(self.wins).min())
129-
130-
def time_rolling_std(self):
131-
(self.sr.rolling(self.wins).std())
132-
133-
def time_rolling_count(self):
134-
(self.sr.rolling(self.wins).count())
135-
136-
def time_rolling_skew(self):
137-
(self.sr.rolling(self.wins).skew())
138-
139-
def time_rolling_kurt(self):
140-
(self.sr.rolling(self.wins).kurt())
141-
142-
def time_rolling_sum(self):
143-
(self.sr.rolling(self.wins).sum())
144-
145-
def time_rolling_corr(self):
146-
(self.srs.rolling(self.wins).corr())
147-
148-
def time_rolling_cov(self):
149-
(self.srs.rolling(self.wins).cov())
150-
151-
def time_rolling_quantile_0_l(self):
152-
(self.sr.rolling(self.winl).quantile(0.0))
153-
154-
def time_rolling_quantile_1_l(self):
155-
(self.sr.rolling(self.winl).quantile(1.0))
156-
157-
def time_rolling_quantile_median_l(self):
158-
(self.sr.rolling(self.winl).quantile(0.5))
159-
160-
def time_rolling_median_l(self):
161-
(self.sr.rolling(self.winl).median())
162-
163-
def time_rolling_mean_l(self):
164-
(self.sr.rolling(self.winl).mean())
165-
166-
def time_rolling_max_l(self):
167-
(self.sr.rolling(self.winl).max())
168-
169-
def time_rolling_min_l(self):
170-
(self.sr.rolling(self.winl).min())
171-
172-
def time_rolling_std_l(self):
173-
(self.sr.rolling(self.wins).std())
174-
175-
def time_rolling_count_l(self):
176-
(self.sr.rolling(self.wins).count())
177-
178-
def time_rolling_skew_l(self):
179-
(self.sr.rolling(self.wins).skew())
180-
181-
def time_rolling_kurt_l(self):
182-
(self.sr.rolling(self.wins).kurt())
183-
184-
def time_rolling_sum_l(self):
185-
(self.sr.rolling(self.wins).sum())
40+
def time_quantile(self, contructor, window, dtype, percentile):
41+
self.roll.quantile(percentile)

doc/source/whatsnew/v0.22.0.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,10 @@ Conversion
263263
- Adding a ``Period`` object to a ``datetime`` or ``Timestamp`` object will now correctly raise a ``TypeError`` (:issue:`17983`)
264264
- Fixed a bug where ``FY5253`` date offsets could incorrectly raise an ``AssertionError`` in arithmetic operatons (:issue:`14774`)
265265
- Bug in :meth:`Index.astype` with a categorical dtype where the resultant index is not converted to a :class:`CategoricalIndex` for all types of index (:issue:`18630`)
266+
- Bug in :meth:`Series.astype` and ``Categorical.astype()`` where an existing categorical data does not get updated (:issue:`10696`, :issue:`18593`)
266267
- Bug in localization of a naive, datetime string in a ``Series`` constructor with a ``datetime[ns, timezone]`` dtype (:issue:`174151`)
267268

269+
268270
Indexing
269271
^^^^^^^^
270272

@@ -278,6 +280,8 @@ Indexing
278280
- Bug in :func:`IntervalIndex.symmetric_difference` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`)
279281
- Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`).
280282
- Bug in tz-aware :class:`DatetimeIndex` where addition/subtraction with a :class:`TimedeltaIndex` or array with ``dtype='timedelta64[ns]'`` was incorrect (:issue:`17558`)
283+
- :func:`Index.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`)
284+
- :func:`DatetimeIndex.to_series` now accepts ``index`` and ``name`` kwargs (:issue:`18699`)
281285

282286
I/O
283287
^^^

pandas/core/categorical.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,12 @@ def astype(self, dtype, copy=True):
436436
437437
"""
438438
if is_categorical_dtype(dtype):
439-
if copy is True:
440-
return self.copy()
441-
return self
439+
# GH 10696/18593
440+
dtype = self.dtype._update_dtype(dtype)
441+
self = self.copy() if copy else self
442+
if dtype == self.dtype:
443+
return self
444+
return self._set_dtype(dtype)
442445
return np.array(self, dtype=dtype, copy=copy)
443446

444447
@cache_readonly

pandas/core/indexes/base.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -983,20 +983,32 @@ def _format_attrs(self):
983983
attrs.append(('length', len(self)))
984984
return attrs
985985

986-
def to_series(self, **kwargs):
986+
def to_series(self, index=None, name=None):
987987
"""
988988
Create a Series with both index and values equal to the index keys
989989
useful with map for returning an indexer based on an index
990990
991+
Parameters
992+
----------
993+
index : Index, optional
994+
index of resulting Series. If None, defaults to original index
995+
name : string, optional
996+
name of resulting Series. If None, defaults to name of original
997+
index
998+
991999
Returns
9921000
-------
9931001
Series : dtype will be based on the type of the Index values.
9941002
"""
9951003

9961004
from pandas import Series
997-
return Series(self._to_embed(),
998-
index=self._shallow_copy(),
999-
name=self.name)
1005+
1006+
if index is None:
1007+
index = self._shallow_copy()
1008+
if name is None:
1009+
name = self.name
1010+
1011+
return Series(self._to_embed(), index=index, name=name)
10001012

10011013
def to_frame(self, index=True):
10021014
"""

pandas/core/indexes/datetimes.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ def _get_time_micros(self):
932932
values = self._local_timestamps()
933933
return fields.get_time_micros(values)
934934

935-
def to_series(self, keep_tz=False):
935+
def to_series(self, keep_tz=False, index=None, name=None):
936936
"""
937937
Create a Series with both index and values equal to the index keys
938938
useful with map for returning an indexer based on an index
@@ -954,15 +954,24 @@ def to_series(self, keep_tz=False):
954954
955955
Series will have a datetime64[ns] dtype. TZ aware
956956
objects will have the tz removed.
957+
index : Index, optional
958+
index of resulting Series. If None, defaults to original index
959+
name : string, optional
960+
name of resulting Series. If None, defaults to name of original
961+
index
957962
958963
Returns
959964
-------
960965
Series
961966
"""
962967
from pandas import Series
963-
return Series(self._to_embed(keep_tz),
964-
index=self._shallow_copy(),
965-
name=self.name)
968+
969+
if index is None:
970+
index = self._shallow_copy()
971+
if name is None:
972+
name = self.name
973+
974+
return Series(self._to_embed(keep_tz), index=index, name=name)
966975

967976
def _to_embed(self, keep_tz=False, dtype=None):
968977
"""

0 commit comments

Comments
 (0)