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 13 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 @@ -386,7 +386,7 @@ def _resolution(self):
return libresolution.resolution(self.asi8, self.tz)

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

def __iter__(self):
"""
Expand Down
112 changes: 51 additions & 61 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,13 +322,49 @@ def start_time(self):
def end_time(self):
return self.to_timestamp(how='end')

def __repr__(self):
return '<{}>\n{}\nLength: {}, dtype: {}'.format(
self.__class__.__name__,
[str(s) for s in self],
len(self),
self.dtype
)
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 __setitem__(
self,
Expand Down Expand Up @@ -456,6 +484,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 +597,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 +620,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 +637,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
3 changes: 3 additions & 0 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,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
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