Skip to content

Commit 3cb91f0

Browse files
committed
CLN: in common.py - revised _maybe_upcast to use _maybe_promote
in rehashpe.py - removed block2d_to_block3d in favor of block2d_to_blocknd
1 parent 0e7c20e commit 3cb91f0

File tree

5 files changed

+26
-64
lines changed

5 files changed

+26
-64
lines changed

pandas/core/common.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def _infer_dtype_from_scalar(val):
651651
# a 1-element ndarray
652652
if isinstance(val, pa.Array):
653653
if val.ndim != 0:
654-
raise ValueError("invalid ndarray passed to _dtype_from_scalar")
654+
raise ValueError("invalid ndarray passed to _infer_dtype_from_scalar")
655655

656656
return val.item(), val.dtype
657657

@@ -719,13 +719,21 @@ def _maybe_promote(dtype, fill_value=np.nan):
719719

720720

721721
def _maybe_upcast(values):
722-
# TODO: convert remaining usage of _maybe_upcast to _maybe_promote
723-
if issubclass(values.dtype.type, np.integer):
724-
values = values.astype(np.float64)
725-
elif issubclass(values.dtype.type, np.bool_):
726-
values = values.astype(np.object_)
722+
""" provide explicty type promotion and coercion """
723+
new_dtype = _maybe_promote(values.dtype)
724+
if new_dtype != values.dtype:
725+
values = values.astype(new_dtype)
727726
return values
728-
727+
728+
def _possibly_cast_item(obj, item, dtype):
729+
chunk = obj[item]
730+
731+
if chunk.values.dtype != dtype:
732+
if dtype in (np.object_, np.bool_):
733+
obj[item] = chunk.astype(np.object_)
734+
elif not issubclass(dtype, (np.integer, np.bool_)): # pragma: no cover
735+
raise ValueError("Unexpected dtype encountered: %s" % dtype)
736+
729737

730738
def _interp_wrapper(f, wrap_dtype, na_override=None):
731739
def wrapper(arr, mask, limit=None):
@@ -927,16 +935,6 @@ def _possibly_cast_to_datetime(value, dtype, coerce = False):
927935
return value
928936

929937

930-
def _possibly_cast_item(obj, item, dtype):
931-
chunk = obj[item]
932-
933-
if chunk.values.dtype != dtype:
934-
if dtype in (np.object_, np.bool_):
935-
obj[item] = chunk.astype(np.object_)
936-
elif not issubclass(dtype, (np.integer, np.bool_)): # pragma: no cover
937-
raise ValueError("Unexpected dtype encountered: %s" % dtype)
938-
939-
940938
def _is_bool_indexer(key):
941939
if isinstance(key, np.ndarray) and key.dtype == np.object_:
942940
key = np.asarray(key)

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ def to_panel(self):
12341234
panel : Panel
12351235
"""
12361236
from pandas.core.panel import Panel
1237-
from pandas.core.reshape import block2d_to_block3d
1237+
from pandas.core.reshape import block2d_to_blocknd
12381238

12391239
# only support this kind for now
12401240
if (not isinstance(self.index, MultiIndex) or
@@ -1261,8 +1261,8 @@ def to_panel(self):
12611261

12621262
new_blocks = []
12631263
for block in selfsorted._data.blocks:
1264-
newb = block2d_to_block3d(block.values.T, block.items, shape,
1265-
major_labels, minor_labels,
1264+
newb = block2d_to_blocknd(block.values.T, block.items, shape,
1265+
[ major_labels, minor_labels ],
12661266
ref_items=selfsorted.columns)
12671267
new_blocks.append(newb)
12681268

pandas/core/reshape.py

+5-40
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from pandas.core.frame import DataFrame
1010

1111
from pandas.core.categorical import Categorical
12-
from pandas.core.common import notnull, _ensure_platform_int
12+
from pandas.core.common import (notnull, _ensure_platform_int, _maybe_promote,
13+
_maybe_upcast)
1314
from pandas.core.groupby import (get_group_index, _compress_group_index,
1415
decons_group_index)
1516
import pandas.core.common as com
@@ -148,11 +149,9 @@ def get_new_values(self):
148149
stride = values.shape[1]
149150
result_width = width * stride
150151

151-
new_values = np.empty((length, result_width), dtype=values.dtype)
152-
new_mask = np.zeros((length, result_width), dtype=bool)
153-
154-
new_values = com._maybe_upcast(new_values)
152+
new_values = np.empty((length, result_width), dtype=_maybe_promote(values.dtype))
155153
new_values.fill(np.nan)
154+
new_mask = np.zeros((length, result_width), dtype=bool)
156155

157156
# is there a simpler / faster way of doing this?
158157
for i in xrange(values.shape[1]):
@@ -761,40 +760,6 @@ def make_axis_dummies(frame, axis='minor', transform=None):
761760
return DataFrame(values, columns=items, index=frame.index)
762761

763762

764-
def block2d_to_block3d(values, items, shape, major_labels, minor_labels,
765-
ref_items=None):
766-
"""
767-
Developer method for pivoting DataFrame -> Panel. Used in HDFStore and
768-
DataFrame.to_panel
769-
"""
770-
from pandas.core.internals import make_block
771-
panel_shape = (len(items),) + shape
772-
773-
# TODO: lexsort depth needs to be 2!!
774-
775-
# Create observation selection vector using major and minor
776-
# labels, for converting to panel format.
777-
selector = minor_labels + shape[1] * major_labels
778-
mask = np.zeros(np.prod(shape), dtype=bool)
779-
mask.put(selector, True)
780-
781-
pvalues = np.empty(panel_shape, dtype=values.dtype)
782-
if not issubclass(pvalues.dtype.type, (np.integer, np.bool_)):
783-
pvalues.fill(np.nan)
784-
elif not mask.all():
785-
pvalues = com._maybe_upcast(pvalues)
786-
pvalues.fill(np.nan)
787-
788-
values = values
789-
for i in xrange(len(items)):
790-
pvalues[i].flat[mask] = values[:, i]
791-
792-
if ref_items is None:
793-
ref_items = items
794-
795-
return make_block(pvalues, items, ref_items)
796-
797-
798763
def block2d_to_blocknd(values, items, shape, labels, ref_items=None):
799764
""" pivot to the labels shape """
800765
from pandas.core.internals import make_block
@@ -812,7 +777,7 @@ def block2d_to_blocknd(values, items, shape, labels, ref_items=None):
812777
if not issubclass(pvalues.dtype.type, (np.integer, np.bool_)):
813778
pvalues.fill(np.nan)
814779
elif not mask.all():
815-
pvalues = com._maybe_upcast(pvalues)
780+
pvalues = _maybe_upcast(pvalues)
816781
pvalues.fill(np.nan)
817782

818783
values = values

pandas/core/series.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import numpy.ma as ma
1616

1717
from pandas.core.common import (isnull, notnull, _is_bool_indexer,
18-
_default_index, _maybe_upcast,
18+
_default_index, _maybe_promote,
1919
_asarray_tuplesafe, is_integer_dtype,
2020
_infer_dtype_from_scalar)
2121
from pandas.core.index import (Index, MultiIndex, InvalidIndexError,
@@ -2818,8 +2818,7 @@ def _get_values():
28182818
return values
28192819

28202820
if offset is None:
2821-
new_values = pa.empty(len(self), dtype=self.dtype)
2822-
new_values = _maybe_upcast(new_values)
2821+
new_values = pa.empty(len(self), dtype=_maybe_promote(self.dtype))
28232822

28242823
if periods > 0:
28252824
new_values[periods:] = self.values[:-periods]

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from pandas.core.categorical import Categorical
2424
from pandas.core.common import _asarray_tuplesafe, _try_sort
2525
from pandas.core.internals import BlockManager, make_block, form_blocks
26-
from pandas.core.reshape import block2d_to_block3d, block2d_to_blocknd, factor_indexer
26+
from pandas.core.reshape import block2d_to_blocknd, factor_indexer
2727
from pandas.core.index import Int64Index
2828
import pandas.core.common as com
2929
from pandas.tools.merge import concat

0 commit comments

Comments
 (0)