Skip to content

Commit

Permalink
CLN: datetimelike arrays: isort, small reorg (pandas-dev#23587)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and tm9k1 committed Nov 19, 2018
1 parent 2b692a0 commit baa9989
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 229 deletions.
8 changes: 6 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ def asi8(self):
# do not cache or you'll create a memory leak
return self._data.view('i8')

# ------------------------------------------------------------------
# Array-like Methods
# ----------------------------------------------------------------
# Array-Like / EA-Interface Methods

@property
def nbytes(self):
return self._data.nbytes

@property
def shape(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def _resolution(self):
return libresolution.resolution(self.asi8, self.tz)

# ----------------------------------------------------------------
# Array-like Methods
# Array-Like / EA-Interface Methods

def __array__(self, dtype=None):
if is_object_dtype(dtype):
Expand Down
106 changes: 52 additions & 54 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,6 @@ def _concat_same_type(cls, to_concat):

# --------------------------------------------------------------------
# Data / Attributes
@property
def nbytes(self):
# TODO(DatetimeArray): remove
return self._data.nbytes

@cache_readonly
def dtype(self):
Expand All @@ -286,10 +282,6 @@ def _ndarray_values(self):
# Ordinals
return self._data

@property
def asi8(self):
return self._data

@property
def freq(self):
"""Return the frequency object for this PeriodArray."""
Expand Down Expand Up @@ -330,6 +322,50 @@ def start_time(self):
def end_time(self):
return self.to_timestamp(how='end')

def to_timestamp(self, freq=None, how='start'):
"""
Cast to DatetimeArray/Index.
Parameters
----------
freq : string or DateOffset, optional
Target frequency. The default is 'D' for week or longer,
'S' otherwise
how : {'s', 'e', 'start', 'end'}
Returns
-------
DatetimeArray/Index
"""
from pandas.core.arrays import DatetimeArrayMixin

how = libperiod._validate_end_alias(how)

end = how == 'E'
if end:
if freq == 'B':
# roll forward to ensure we land on B date
adjust = Timedelta(1, 'D') - Timedelta(1, 'ns')
return self.to_timestamp(how='start') + adjust
else:
adjust = Timedelta(1, 'ns')
return (self + self.freq).to_timestamp(how='start') - adjust

if freq is None:
base, mult = frequencies.get_freq_code(self.freq)
freq = frequencies.get_to_timestamp_base(base)
else:
freq = Period._maybe_convert_freq(freq)

base, mult = frequencies.get_freq_code(freq)
new_data = self.asfreq(freq, how=how)

new_data = libperiod.periodarr_to_dt64arr(new_data.asi8, base)
return DatetimeArrayMixin(new_data, freq='infer')

# --------------------------------------------------------------------
# Array-like / EA-Interface Methods

def __repr__(self):
return '<{}>\n{}\nLength: {}, dtype: {}'.format(
self.__class__.__name__,
Expand Down Expand Up @@ -456,6 +492,8 @@ def value_counts(self, dropna=False):
name=result.index.name)
return Series(result.values, index=index, name=result.name)

# --------------------------------------------------------------------

def shift(self, periods=1):
"""
Shift values by desired number.
Expand Down Expand Up @@ -567,49 +605,9 @@ def asfreq(self, freq=None, how='E'):

return type(self)(new_data, freq=freq)

def to_timestamp(self, freq=None, how='start'):
"""
Cast to DatetimeArray/Index
Parameters
----------
freq : string or DateOffset, optional
Target frequency. The default is 'D' for week or longer,
'S' otherwise
how : {'s', 'e', 'start', 'end'}
Returns
-------
DatetimeArray/Index
"""
from pandas.core.arrays import DatetimeArrayMixin

how = libperiod._validate_end_alias(how)

end = how == 'E'
if end:
if freq == 'B':
# roll forward to ensure we land on B date
adjust = Timedelta(1, 'D') - Timedelta(1, 'ns')
return self.to_timestamp(how='start') + adjust
else:
adjust = Timedelta(1, 'ns')
return (self + self.freq).to_timestamp(how='start') - adjust

if freq is None:
base, mult = frequencies.get_freq_code(self.freq)
freq = frequencies.get_to_timestamp_base(base)
else:
freq = Period._maybe_convert_freq(freq)

base, mult = frequencies.get_freq_code(freq)
new_data = self.asfreq(freq, how=how)

new_data = libperiod.periodarr_to_dt64arr(new_data.asi8, base)
return DatetimeArrayMixin(new_data, freq='infer')

# ------------------------------------------------------------------
# Formatting

def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):
""" actually format my specific types """
# TODO(DatetimeArray): remove
Expand All @@ -630,9 +628,13 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):
values = np.array([formatter(dt) for dt in values])
return values

# Delegation...
def strftime(self, date_format):
return self._format_native_types(date_format=date_format)

def repeat(self, repeats, *args, **kwargs):
"""
Repeat elements of a Categorical.
Repeat elements of a PeriodArray.
See also
--------
Expand All @@ -643,10 +645,6 @@ def repeat(self, repeats, *args, **kwargs):
values = self._data.repeat(repeats)
return type(self)(values, self.freq)

# Delegation...
def strftime(self, date_format):
return self._format_native_types(date_format=date_format)

def astype(self, dtype, copy=True):
# TODO: Figure out something better here...
# We have DatetimeLikeArrayMixin ->
Expand Down
26 changes: 19 additions & 7 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ def _generate_range(cls, start, end, periods, freq, closed=None):

return cls._simple_new(index, freq=freq)

# ----------------------------------------------------------------
# Array-Like / EA-Interface Methods

# ----------------------------------------------------------------
# Arithmetic Methods

Expand Down Expand Up @@ -412,20 +415,25 @@ def sequence_to_td64ns(data, copy=False, unit="ns", errors="raise"):
array : list-like
copy : bool, default False
unit : str, default "ns"
The timedelta unit to treat integers as multiples of.
errors : {"raise", "coerce", "ignore"}, default "raise"
How to handle elements that cannot be converted to timedelta64[ns].
See ``pandas.to_timedelta`` for details.
Returns
-------
ndarray[timedelta64[ns]]
converted : numpy.ndarray
The sequence converted to a numpy array with dtype ``timedelta64[ns]``.
inferred_freq : Tick or None
The inferred frequency of the sequence.
Raises
------
ValueError : data cannot be converted to timedelta64[ns]
ValueError : Data cannot be converted to timedelta64[ns].
Notes
-----
Unlike `pandas.to_timedelta`, if setting `errors=ignore` will not cause
Unlike `pandas.to_timedelta`, if setting ``errors=ignore`` will not cause
errors to be ignored; they are caught and subsequently ignored at a
higher level.
"""
Expand Down Expand Up @@ -497,12 +505,13 @@ def ints_to_td64ns(data, unit="ns"):
Parameters
----------
data : np.ndarray with integer-dtype
data : numpy.ndarray with integer-dtype
unit : str, default "ns"
The timedelta unit to treat integers as multiples of.
Returns
-------
ndarray[timedelta64[ns]]
numpy.ndarray : timedelta64[ns] array converted from data
bool : whether a copy was made
"""
copy_made = False
Expand Down Expand Up @@ -538,15 +547,18 @@ def objects_to_td64ns(data, unit="ns", errors="raise"):
----------
data : ndarray or Index
unit : str, default "ns"
The timedelta unit to treat integers as multiples of.
errors : {"raise", "coerce", "ignore"}, default "raise"
How to handle elements that cannot be converted to timedelta64[ns].
See ``pandas.to_timedelta`` for details.
Returns
-------
ndarray[timedelta64[ns]]
numpy.ndarray : timedelta64[ns] array converted from data
Raises
------
ValueError : data cannot be converted to timedelta64[ns]
ValueError : Data cannot be converted to timedelta64[ns].
Notes
-----
Expand Down
44 changes: 16 additions & 28 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,32 @@
"""
import warnings

from pandas import compat
from pandas.compat.numpy import function as nv
from pandas.core.tools.timedeltas import to_timedelta

import numpy as np

from pandas._libs import lib, iNaT, NaT
from pandas._libs.tslibs.timestamps import round_nsint64, RoundTo
from pandas._libs import NaT, iNaT, lib
from pandas._libs.tslibs.timestamps import RoundTo, round_nsint64
import pandas.compat as compat
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, cache_readonly

from pandas.core.dtypes.common import (
ensure_int64,
is_dtype_equal,
is_float,
is_integer,
is_list_like,
is_scalar,
is_bool_dtype,
is_period_dtype,
is_categorical_dtype,
is_datetime_or_timedelta_dtype,
is_float_dtype,
is_integer_dtype,
is_object_dtype,
is_string_dtype)
from pandas.core.dtypes.generic import (
ABCIndex, ABCSeries, ABCIndexClass)
ensure_int64, is_bool_dtype, is_categorical_dtype,
is_datetime_or_timedelta_dtype, is_dtype_equal, is_float, is_float_dtype,
is_integer, is_integer_dtype, is_list_like, is_object_dtype,
is_period_dtype, is_scalar, is_string_dtype)
import pandas.core.dtypes.concat as _concat
from pandas.core.dtypes.generic import ABCIndex, ABCIndexClass, ABCSeries
from pandas.core.dtypes.missing import isna
from pandas.core import common as com, algorithms, ops

import pandas.io.formats.printing as printing

from pandas.core import algorithms, common as com, ops
from pandas.core.arrays import PeriodArray
from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import Index, _index_shared_docs
from pandas.util._decorators import Appender, cache_readonly
import pandas.core.dtypes.concat as _concat
from pandas.core.tools.timedeltas import to_timedelta

import pandas.io.formats.printing as printing

import pandas.core.indexes.base as ibase
_index_doc_kwargs = dict(ibase._index_doc_kwargs)


Expand Down
Loading

0 comments on commit baa9989

Please sign in to comment.