Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 54 additions & 45 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,11 @@ def _take_nd_generic(arr, indexer, out, axis, fill_value, mask_info):
('float64', 'float64'): algos.take_1d_float64_float64,
('object', 'object'): algos.take_1d_object_object,
('bool', 'bool'):
_view_wrapper(algos.take_1d_bool_bool, np.uint8, np.uint8),
_view_wrapper(algos.take_1d_bool_bool, np.uint8, np.uint8),
('bool', 'object'):
_view_wrapper(algos.take_1d_bool_object, np.uint8, None),
('datetime64[ns]','datetime64[ns]'):
_view_wrapper(algos.take_1d_int64_int64, np.int64, np.int64, np.int64)
_view_wrapper(algos.take_1d_bool_object, np.uint8, None),
('datetime64[ns]', 'datetime64[ns]'):
_view_wrapper(algos.take_1d_int64_int64, np.int64, np.int64, np.int64)
}


Expand All @@ -351,12 +351,12 @@ def _take_nd_generic(arr, indexer, out, axis, fill_value, mask_info):
('float64', 'float64'): algos.take_2d_axis0_float64_float64,
('object', 'object'): algos.take_2d_axis0_object_object,
('bool', 'bool'):
_view_wrapper(algos.take_2d_axis0_bool_bool, np.uint8, np.uint8),
_view_wrapper(algos.take_2d_axis0_bool_bool, np.uint8, np.uint8),
('bool', 'object'):
_view_wrapper(algos.take_2d_axis0_bool_object, np.uint8, None),
('datetime64[ns]','datetime64[ns]'):
_view_wrapper(algos.take_2d_axis0_int64_int64, np.int64, np.int64,
fill_wrap=np.int64)
_view_wrapper(algos.take_2d_axis0_bool_object, np.uint8, None),
('datetime64[ns]', 'datetime64[ns]'):
_view_wrapper(algos.take_2d_axis0_int64_int64, np.int64, np.int64,
fill_wrap=np.int64)
}


Expand All @@ -379,12 +379,12 @@ def _take_nd_generic(arr, indexer, out, axis, fill_value, mask_info):
('float64', 'float64'): algos.take_2d_axis1_float64_float64,
('object', 'object'): algos.take_2d_axis1_object_object,
('bool', 'bool'):
_view_wrapper(algos.take_2d_axis1_bool_bool, np.uint8, np.uint8),
_view_wrapper(algos.take_2d_axis1_bool_bool, np.uint8, np.uint8),
('bool', 'object'):
_view_wrapper(algos.take_2d_axis1_bool_object, np.uint8, None),
('datetime64[ns]','datetime64[ns]'):
_view_wrapper(algos.take_2d_axis1_int64_int64, np.int64, np.int64,
fill_wrap=np.int64)
_view_wrapper(algos.take_2d_axis1_bool_object, np.uint8, None),
('datetime64[ns]', 'datetime64[ns]'):
_view_wrapper(algos.take_2d_axis1_int64_int64, np.int64, np.int64,
fill_wrap=np.int64)
}


Expand All @@ -407,12 +407,12 @@ def _take_nd_generic(arr, indexer, out, axis, fill_value, mask_info):
('float64', 'float64'): algos.take_2d_multi_float64_float64,
('object', 'object'): algos.take_2d_multi_object_object,
('bool', 'bool'):
_view_wrapper(algos.take_2d_multi_bool_bool, np.uint8, np.uint8),
_view_wrapper(algos.take_2d_multi_bool_bool, np.uint8, np.uint8),
('bool', 'object'):
_view_wrapper(algos.take_2d_multi_bool_object, np.uint8, None),
('datetime64[ns]','datetime64[ns]'):
_view_wrapper(algos.take_2d_multi_int64_int64, np.int64, np.int64,
fill_wrap=np.int64)
_view_wrapper(algos.take_2d_multi_bool_object, np.uint8, None),
('datetime64[ns]', 'datetime64[ns]'):
_view_wrapper(algos.take_2d_multi_int64_int64, np.int64, np.int64,
fill_wrap=np.int64)
}


Expand Down Expand Up @@ -440,7 +440,7 @@ def _get_take_nd_function(ndim, arr_dtype, out_dtype, axis=0, mask_info=None):
if func is not None:
func = _convert_wrapper(func, out_dtype)
return func

def func(arr, indexer, out, fill_value=np.nan):
_take_nd_generic(arr, indexer, out, axis=axis,
fill_value=fill_value, mask_info=mask_info)
Expand Down Expand Up @@ -645,10 +645,11 @@ def _infer_dtype_from_scalar(val):
# a 1-element ndarray
if isinstance(val, pa.Array):
if val.ndim != 0:
raise ValueError("invalid ndarray passed to _infer_dtype_from_scalar")
raise ValueError(
"invalid ndarray passed to _infer_dtype_from_scalar")

dtype = val.dtype
val = val.item()
val = val.item()

elif isinstance(val, basestring):

Expand All @@ -662,7 +663,7 @@ def _infer_dtype_from_scalar(val):

elif isinstance(val, np.datetime64):
# ugly hacklet
val = lib.Timestamp(val).value
val = lib.Timestamp(val).value
dtype = np.dtype('M8[ns]')

elif is_bool(val):
Expand Down Expand Up @@ -695,7 +696,7 @@ def _maybe_promote(dtype, fill_value=np.nan):
except:
# the proper thing to do here would probably be to upcast to
# object (but numpy 1.6.1 doesn't do this properly)
fill_value = tslib.iNaT
fill_value = tslib.iNaT
elif is_float(fill_value):
if issubclass(dtype.type, np.bool_):
dtype = np.object_
Expand Down Expand Up @@ -760,10 +761,10 @@ def wrapper(arr, mask, limit=None):

def pad_1d(values, limit=None, mask=None):

dtype = values.dtype.name
dtype = values.dtype.name
_method = None
if is_float_dtype(values):
_method = getattr(algos,'pad_inplace_%s' % dtype,None)
_method = getattr(algos, 'pad_inplace_%s' % dtype, None)
elif is_datetime64_dtype(values):
_method = _pad_1d_datetime
elif values.dtype == np.object_:
Expand All @@ -780,10 +781,10 @@ def pad_1d(values, limit=None, mask=None):

def backfill_1d(values, limit=None, mask=None):

dtype = values.dtype.name
dtype = values.dtype.name
_method = None
if is_float_dtype(values):
_method = getattr(algos,'backfill_inplace_%s' % dtype,None)
_method = getattr(algos, 'backfill_inplace_%s' % dtype, None)
elif is_datetime64_dtype(values):
_method = _backfill_1d_datetime
elif values.dtype == np.object_:
Expand All @@ -801,10 +802,10 @@ def backfill_1d(values, limit=None, mask=None):

def pad_2d(values, limit=None, mask=None):

dtype = values.dtype.name
dtype = values.dtype.name
_method = None
if is_float_dtype(values):
_method = getattr(algos,'pad_2d_inplace_%s' % dtype,None)
_method = getattr(algos, 'pad_2d_inplace_%s' % dtype, None)
elif is_datetime64_dtype(values):
_method = _pad_2d_datetime
elif values.dtype == np.object_:
Expand All @@ -826,10 +827,10 @@ def pad_2d(values, limit=None, mask=None):

def backfill_2d(values, limit=None, mask=None):

dtype = values.dtype.name
dtype = values.dtype.name
_method = None
if is_float_dtype(values):
_method = getattr(algos,'backfill_2d_inplace_%s' % dtype,None)
_method = getattr(algos, 'backfill_2d_inplace_%s' % dtype, None)
elif is_datetime64_dtype(values):
_method = _backfill_2d_datetime
elif values.dtype == np.object_:
Expand Down Expand Up @@ -868,20 +869,23 @@ def _possibly_convert_objects(values, convert_dates=True, convert_numeric=True):

# we take an aggressive stance and convert to datetime64[ns]
if convert_dates == 'coerce':
new_values = _possibly_cast_to_datetime(values, 'M8[ns]', coerce = True)
new_values = _possibly_cast_to_datetime(
values, 'M8[ns]', coerce=True)

# if we are all nans then leave me alone
if not isnull(new_values).all():
values = new_values

else:
values = lib.maybe_convert_objects(values, convert_datetime=convert_dates)
values = lib.maybe_convert_objects(
values, convert_datetime=convert_dates)

# convert to numeric
if convert_numeric and values.dtype == np.object_:
try:
new_values = lib.maybe_convert_numeric(values,set(),coerce_numeric=True)

new_values = lib.maybe_convert_numeric(
values, set(), coerce_numeric=True)

# if we are all nans then leave me alone
if not isnull(new_values).all():
values = new_values
Expand All @@ -891,10 +895,11 @@ def _possibly_convert_objects(values, convert_dates=True, convert_numeric=True):

return values


def _possibly_convert_platform(values):
""" try to do platform conversion, allow ndarray or list here """

if isinstance(values, (list,tuple)):
if isinstance(values, (list, tuple)):
values = lib.list_to_object_array(values)
if values.dtype == np.object_:
values = lib.maybe_convert_objects(values)
Expand All @@ -906,17 +911,18 @@ def _possibly_cast_to_timedelta(value):
""" try to cast to timedelta64 w/o coercion """
new_value = tslib.array_to_timedelta64(value.astype(object), coerce=False)
if new_value.dtype == 'i8':
value = np.array(new_value,dtype='timedelta64[ns]')
value = np.array(new_value, dtype='timedelta64[ns]')
return value

def _possibly_cast_to_datetime(value, dtype, coerce = False):

def _possibly_cast_to_datetime(value, dtype, coerce=False):
""" try to cast the array/value to a datetimelike dtype, converting float nan to iNaT """

if isinstance(dtype, basestring):
dtype = np.dtype(dtype)

if dtype is not None:
is_datetime64 = is_datetime64_dtype(dtype)
is_datetime64 = is_datetime64_dtype(dtype)
is_timedelta64 = is_timedelta64_dtype(dtype)

if is_datetime64 or is_timedelta64:
Expand All @@ -935,7 +941,8 @@ def _possibly_cast_to_datetime(value, dtype, coerce = False):
elif np.prod(value.shape) and value.dtype != dtype:
try:
if is_datetime64:
value = tslib.array_to_datetime(value, coerce = coerce)
value = tslib.array_to_datetime(
value, coerce=coerce)
elif is_timedelta64:
value = _possibly_cast_to_timedelta(value)
except:
Expand All @@ -946,7 +953,7 @@ def _possibly_cast_to_datetime(value, dtype, coerce = False):
# don't change the value unless we find a datetime set
v = value
if not is_list_like(v):
v = [ v ]
v = [v]
if len(v):
inferred_type = lib.infer_dtype(v)
if inferred_type == 'datetime':
Expand Down Expand Up @@ -1127,13 +1134,14 @@ def banner(message):
bar = '=' * 80
return '%s\n%s\n%s' % (bar, message, bar)


def _long_prod(vals):
result = 1L
for x in vals:
result *= x
return result


class groupby(dict):
"""
A simple groupby different from the one in itertools.
Expand Down Expand Up @@ -1317,7 +1325,8 @@ def is_float_dtype(arr_or_dtype):


def is_list_like(arg):
return hasattr(arg, '__iter__') and not isinstance(arg, basestring) or hasattr(arg,'len')
return hasattr(arg, '__iter__') and not isinstance(arg, basestring) or hasattr(arg, 'len')


def _is_sequence(x):
try:
Expand All @@ -1336,7 +1345,7 @@ def _is_sequence(x):
_ensure_object = algos.ensure_object


def _astype_nansafe(arr, dtype, copy = True):
def _astype_nansafe(arr, dtype, copy=True):
""" return a view if copy is False """
if not isinstance(dtype, np.dtype):
dtype = np.dtype(dtype)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ def _build_option_description(k):

s = u'%s: ' % k
if o:
s += u'[default: %s] [currently: %s]' % (o.defval, _get_option(k, True))
s += u'[default: %s] [currently: %s]' % (
o.defval, _get_option(k, True))

if o.doc:
s += '\n' + '\n '.join(o.doc.strip().split('\n'))
Expand Down
22 changes: 13 additions & 9 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ def _get_footer(self):
footer += 'Length: %d' % len(self.series)

if self.dtype:
if getattr(self.series.dtype,'name',None):
if getattr(self.series.dtype, 'name', None):
if footer:
footer += ', '
footer += 'Dtype: %s' % com.pprint_thing(self.series.dtype.name)
footer += 'Dtype: %s' % com.pprint_thing(
self.series.dtype.name)

return unicode(footer)

Expand Down Expand Up @@ -516,7 +517,7 @@ def _write_cell(self, s, kind='td', indent=0, tags=None):
else:
start_tag = '<%s>' % kind

esc = {'<' : r'&lt;', '>' : r'&gt;'}
esc = {'<': r'&lt;', '>': r'&gt;'}
rs = com.pprint_thing(s, escape_chars=esc)
self.write(
'%s%s</%s>' % (start_tag, rs, kind), indent)
Expand Down Expand Up @@ -1102,19 +1103,19 @@ def __init__(self, *args, **kwargs):
def _format_with(self, fmt_str):
def _val(x, threshold):
if notnull(x):
if threshold is None or abs(x) > get_option("display.chop_threshold"):
return fmt_str % x
if threshold is None or abs(x) > get_option("display.chop_threshold"):
return fmt_str % x
else:
if fmt_str.endswith("e"): # engineering format
return "0"
if fmt_str.endswith("e"): # engineering format
return "0"
else:
return fmt_str % 0
return fmt_str % 0
else:

return self.na_rep

threshold = get_option("display.chop_threshold")
fmt_values = [ _val(x, threshold) for x in self.values]
fmt_values = [_val(x, threshold) for x in self.values]
return _trim_zeros(fmt_values, self.na_rep)

def get_result(self):
Expand Down Expand Up @@ -1172,6 +1173,7 @@ def get_result(self):
fmt_values = [formatter(x) for x in self.values]
return _make_fixed_width(fmt_values, self.justify)


def _format_datetime64(x, tz=None):
if isnull(x):
return 'NaT'
Expand All @@ -1192,12 +1194,14 @@ def get_result(self):
fmt_values = [formatter(x) for x in self.values]
return _make_fixed_width(fmt_values, self.justify)


def _format_timedelta64(x):
if isnull(x):
return 'NaT'

return lib.repr_timedelta64(x)


def _make_fixed_width(strings, justify='right', minimum=None):
if len(strings) == 0:
return strings
Expand Down
Loading