Skip to content

Commit b7238e6

Browse files
committed
add ability to specify keywords to astype for creation defaults
1 parent 8049354 commit b7238e6

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

doc/source/whatsnew/v0.16.0.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@ New Behavior
355355
cat
356356
cat.ordered
357357

358+
For ease of creation of series of ``Categoricals``, we have added the ability to pass keywords when calling ``.astype()``, these
359+
are passed directly to the constructor.
360+
361+
.. ipython:: python
362+
363+
s = Series(["a","b","c","a"]).astype('category',ordered=True)
364+
s
365+
s = Series(["a","b","c","a"]).astype('category',categories=list('abcdef'),ordered=False)
366+
s
367+
358368
Indexing Changes
359369
~~~~~~~~~~~~~~~~
360370

pandas/core/generic.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ def blocks(self):
22072207
"Internal property, property synonym for as_blocks()"
22082208
return self.as_blocks()
22092209

2210-
def astype(self, dtype, copy=True, raise_on_error=True):
2210+
def astype(self, dtype, copy=True, raise_on_error=True, **kwargs):
22112211
"""
22122212
Cast object to input numpy.dtype
22132213
Return a copy when copy = True (be really careful with this!)
@@ -2216,14 +2216,15 @@ def astype(self, dtype, copy=True, raise_on_error=True):
22162216
----------
22172217
dtype : numpy.dtype or Python type
22182218
raise_on_error : raise on invalid input
2219+
kwargs : keyword arguments to pass on to the constructor
22192220
22202221
Returns
22212222
-------
22222223
casted : type of caller
22232224
"""
22242225

22252226
mgr = self._data.astype(
2226-
dtype=dtype, copy=copy, raise_on_error=raise_on_error)
2227+
dtype=dtype, copy=copy, raise_on_error=raise_on_error, **kwargs)
22272228
return self._constructor(mgr).__finalize__(self)
22282229

22292230
def copy(self, deep=True):

pandas/core/internals.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,12 @@ def downcast(self, dtypes=None):
367367

368368
return blocks
369369

370-
def astype(self, dtype, copy=False, raise_on_error=True, values=None):
370+
def astype(self, dtype, copy=False, raise_on_error=True, values=None, **kwargs):
371371
return self._astype(dtype, copy=copy, raise_on_error=raise_on_error,
372-
values=values)
372+
values=values, **kwargs)
373373

374374
def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
375-
klass=None):
375+
klass=None, **kwargs):
376376
"""
377377
Coerce to the new type (if copy=True, return a new copy)
378378
raise on an except if raise == True
@@ -381,7 +381,7 @@ def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
381381
# may need to convert to categorical
382382
# this is only called for non-categoricals
383383
if self.is_categorical_astype(dtype):
384-
return make_block(Categorical(self.values),
384+
return make_block(Categorical(self.values, **kwargs),
385385
ndim=self.ndim,
386386
placement=self.mgr_locs)
387387

@@ -1229,8 +1229,8 @@ def to_native_types(self, slicer=None, na_rep='', float_format=None, decimal='.'
12291229
values = np.array(values, dtype=object)
12301230
mask = isnull(values)
12311231
values[mask] = na_rep
1232-
1233-
1232+
1233+
12341234
if float_format and decimal != '.':
12351235
formatter = lambda v : (float_format % v).replace('.',decimal,1)
12361236
elif decimal != '.':
@@ -1239,12 +1239,12 @@ def to_native_types(self, slicer=None, na_rep='', float_format=None, decimal='.'
12391239
formatter = lambda v : float_format % v
12401240
else:
12411241
formatter = None
1242-
1242+
12431243
if formatter:
12441244
imask = (~mask).ravel()
12451245
values.flat[imask] = np.array(
12461246
[formatter(val) for val in values.ravel()[imask]])
1247-
1247+
12481248
return values.tolist()
12491249

12501250
def should_store(self, value):

pandas/tests/test_categorical.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,17 @@ def test_creation_astype(self):
11951195
df["cats"] = df["cats"].astype("category")
11961196
tm.assert_frame_equal(exp_df, df)
11971197

1198+
# with keywords
1199+
l = ["a","b","c","a"]
1200+
s = pd.Series(l)
1201+
exp = pd.Series(Categorical(l, ordered=True))
1202+
res = s.astype('category', ordered=True)
1203+
tm.assert_series_equal(res, exp)
1204+
1205+
exp = pd.Series(Categorical(l, categories=list('abcdef'), ordered=True))
1206+
res = s.astype('category', categories=list('abcdef'), ordered=True)
1207+
tm.assert_series_equal(res, exp)
1208+
11981209
def test_construction_series(self):
11991210

12001211
l = [1,2,3,1]

0 commit comments

Comments
 (0)