Skip to content

Commit f2cd3ba

Browse files
committed
CLN: add fill_value return value to common._maybe_promote
1 parent 9faa8de commit f2cd3ba

File tree

2 files changed

+20
-68
lines changed

2 files changed

+20
-68
lines changed

pandas/core/common.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ def _maybe_promote(dtype, fill_value=np.nan):
705705
# object (but numpy 1.6.1 doesn't do this properly)
706706
fill_value = tslib.iNaT
707707
elif is_float(fill_value):
708+
if fill_value is None:
709+
fill_value = np.nan
708710
if issubclass(dtype.type, np.bool_):
709711
dtype = np.object_
710712
elif issubclass(dtype.type, np.integer):
@@ -729,17 +731,18 @@ def _maybe_promote(dtype, fill_value=np.nan):
729731
dtype = np.object_
730732
return dtype, fill_value
731733

734+
732735
def _maybe_upcast(values, fill_value=np.nan, copy=False):
733736
""" provide explicty type promotion and coercion
734737
if copy == True, then a copy is created even if no upcast is required """
735-
736738
new_dtype, fill_value = _maybe_promote(values.dtype, fill_value)
737739
if new_dtype != values.dtype:
738740
values = values.astype(new_dtype)
739741
elif copy:
740742
values = values.copy()
741743
return values, fill_value
742744

745+
743746
def _possibly_cast_item(obj, item, dtype):
744747
chunk = obj[item]
745748

@@ -950,6 +953,22 @@ def _possibly_cast_to_datetime(value, dtype, coerce = False):
950953
return value
951954

952955

956+
<<<<<<< HEAD
957+
=======
958+
def _infer_dtype(value):
959+
if isinstance(value, (float, np.floating)):
960+
return np.float64
961+
elif isinstance(value, (bool, np.bool_)):
962+
return np.bool_
963+
elif isinstance(value, (int, long, np.integer)):
964+
return np.int64
965+
elif isinstance(value, (complex, np.complexfloating)):
966+
return np.complex128
967+
else:
968+
return np.object_
969+
970+
971+
>>>>>>> e2b4ccd... CLN: add fill_value return value to common._maybe_promote
953972
def _is_bool_indexer(key):
954973
if isinstance(key, np.ndarray) and key.dtype == np.object_:
955974
key = np.asarray(key)

pandas/tests/test_frame.py

-67
Original file line numberDiff line numberDiff line change
@@ -8311,73 +8311,6 @@ def test_constructor_with_convert(self):
83118311
None], np.object_))
83128312
assert_series_equal(result, expected)
83138313

8314-
def test_constructor_with_datetimes(self):
8315-
intname = np.dtype(np.int_).name
8316-
floatname = np.dtype(np.float_).name
8317-
datetime64name = np.dtype('M8[ns]').name
8318-
objectname = np.dtype(np.object_).name
8319-
8320-
# single item
8321-
df = DataFrame({'A' : 1, 'B' : 'foo', 'C' : 'bar', 'D' : Timestamp("20010101"), 'E' : datetime(2001,1,2,0,0) },
8322-
index=np.arange(10))
8323-
result = df.get_dtype_counts()
8324-
expected = Series({intname: 1, datetime64name: 2, objectname : 2})
8325-
result.sort()
8326-
expected.sort()
8327-
assert_series_equal(result, expected)
8328-
8329-
# check with ndarray construction ndim==0 (e.g. we are passing a ndim 0 ndarray with a dtype specified)
8330-
df = DataFrame({'a': 1., 'b': 2, 'c': 'foo', floatname : np.array(1.,dtype=floatname),
8331-
intname : np.array(1,dtype=intname)}, index=np.arange(10))
8332-
result = df.get_dtype_counts()
8333-
expected = Series({intname: 2, floatname : 2, objectname : 1})
8334-
result.sort()
8335-
expected.sort()
8336-
assert_series_equal(result, expected)
8337-
8338-
# check with ndarray construction ndim>0
8339-
df = DataFrame({'a': 1., 'b': 2, 'c': 'foo', floatname : np.array([1.]*10,dtype=floatname),
8340-
intname : np.array([1]*10,dtype=intname)}, index=np.arange(10))
8341-
result = df.get_dtype_counts()
8342-
expected = Series({intname: 2, floatname : 2, objectname : 1})
8343-
result.sort()
8344-
expected.sort()
8345-
assert_series_equal(result, expected)
8346-
8347-
# GH #2751 (construction with no index specified)
8348-
df = DataFrame({'a':[1,2,4,7], 'b':[1.2, 2.3, 5.1, 6.3], 'c':list('abcd'), 'd':[datetime(2000,1,1) for i in range(4)] })
8349-
result = df.get_dtype_counts()
8350-
# TODO: fix this on 32-bit (or decide it's ok behavior?)
8351-
# expected = Series({intname: 1, floatname : 1, datetime64name: 1, objectname : 1})
8352-
expected = Series({'int64': 1, floatname : 1, datetime64name: 1, objectname : 1})
8353-
result.sort()
8354-
expected.sort()
8355-
assert_series_equal(result, expected)
8356-
8357-
# GH 2809
8358-
from pandas import date_range
8359-
ind = date_range(start="2000-01-01", freq="D", periods=10)
8360-
datetimes = [ts.to_pydatetime() for ts in ind]
8361-
datetime_s = Series(datetimes)
8362-
self.assert_(datetime_s.dtype == 'M8[ns]')
8363-
df = DataFrame({'datetime_s':datetime_s})
8364-
result = df.get_dtype_counts()
8365-
expected = Series({ datetime64name : 1 })
8366-
result.sort()
8367-
expected.sort()
8368-
assert_series_equal(result, expected)
8369-
8370-
# GH 2810
8371-
ind = date_range(start="2000-01-01", freq="D", periods=10)
8372-
datetimes = [ts.to_pydatetime() for ts in ind]
8373-
dates = [ts.date() for ts in ind]
8374-
df = DataFrame({'datetimes': datetimes, 'dates':dates})
8375-
result = df.get_dtype_counts()
8376-
expected = Series({ datetime64name : 1, objectname : 1 })
8377-
result.sort()
8378-
expected.sort()
8379-
assert_series_equal(result, expected)
8380-
83818314
def test_constructor_frame_copy(self):
83828315
cop = DataFrame(self.frame, copy=True)
83838316
cop['A'] = 5

0 commit comments

Comments
 (0)