Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLN: datetimelike arrays: isort, small reorg #23587

Merged
merged 21 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e3267bc
import mixins without mixins in names
jbrockmendel Nov 8, 2018
d4bde65
isort timedeltas, datetimelike, datetime index files
jbrockmendel Nov 8, 2018
a2754c1
Section header
jbrockmendel Nov 8, 2018
e81f5d6
implement repr, with tests, truncate long reprs for periodarray, rear…
jbrockmendel Nov 8, 2018
1647ea2
isort
jbrockmendel Nov 9, 2018
c99ca32
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Nov 9, 2018
db1d446
remove repr changes
TomAugspurger Nov 9, 2018
1355f59
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Nov 9, 2018
14d5e76
isort fixup
jbrockmendel Nov 10, 2018
a0c7bf4
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Nov 10, 2018
edab7a2
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Nov 11, 2018
dd85053
typo fixup
jbrockmendel Nov 11, 2018
e0c3fcf
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Nov 11, 2018
6fe6496
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Nov 11, 2018
469ac83
remove repr tests
jbrockmendel Nov 11, 2018
65209f1
fixup imports
jbrockmendel Nov 11, 2018
0ada030
flake8 fixups
jbrockmendel Nov 11, 2018
58dedcf
Merge branch 'master' of https://github.com/pandas-dev/pandas into dl…
jbrockmendel Nov 11, 2018
0a17757
docstring suggestions
jbrockmendel Nov 11, 2018
4ac9576
fixup unused import
jbrockmendel Nov 12, 2018
c7211f7
undo remaining repr changes
jorisvandenbossche Nov 12, 2018
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but for datetimes the _data is not integer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inherited implementation returns self._data.view('i8'). The view is redundant for PeriodArray, but still correct


@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'):
jreback marked this conversation as resolved.
Show resolved Hide resolved
"""
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)

# ----------------------------------------------------------------
jbrockmendel marked this conversation as resolved.
Show resolved Hide resolved
# 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