Skip to content

Commit 723dad8

Browse files
committed
Merge branch 'master' into PR_TOOL_MERGE_PR_19714
2 parents b6df1c8 + e8b80b1 commit 723dad8

File tree

20 files changed

+113
-161
lines changed

20 files changed

+113
-161
lines changed

doc/source/basics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2312,4 +2312,4 @@ All NumPy dtypes are subclasses of ``numpy.generic``:
23122312
.. note::
23132313

23142314
Pandas also defines the types ``category``, and ``datetime64[ns, tz]``, which are not integrated into the normal
2315-
NumPy hierarchy and wont show up with the above function.
2315+
NumPy hierarchy and won't show up with the above function.

doc/source/dsintro.rst

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,19 @@ and returns a DataFrame. It operates like the ``DataFrame`` constructor except
364364
for the ``orient`` parameter which is ``'columns'`` by default, but which can be
365365
set to ``'index'`` in order to use the dict keys as row labels.
366366

367+
368+
.. ipython:: python
369+
370+
pd.DataFrame.from_dict(dict([('A', [1, 2, 3]), ('B', [4, 5, 6])]))
371+
372+
If you pass ``orient='index'``, the keys will be the row labels. In this
373+
case, you can also pass the desired column names:
374+
375+
.. ipython:: python
376+
377+
pd.DataFrame.from_dict(dict([('A', [1, 2, 3]), ('B', [4, 5, 6])]),
378+
orient='index', columns=['one', 'two', 'three'])
379+
367380
.. _basics.dataframe.from_records:
368381

369382
**DataFrame.from_records**
@@ -378,28 +391,6 @@ dtype. For example:
378391
data
379392
pd.DataFrame.from_records(data, index='C')
380393
381-
.. _basics.dataframe.from_items:
382-
383-
**DataFrame.from_items**
384-
385-
``DataFrame.from_items`` works analogously to the form of the ``dict``
386-
constructor that takes a sequence of ``(key, value)`` pairs, where the keys are
387-
column (or row, in the case of ``orient='index'``) names, and the value are the
388-
column values (or row values). This can be useful for constructing a DataFrame
389-
with the columns in a particular order without having to pass an explicit list
390-
of columns:
391-
392-
.. ipython:: python
393-
394-
pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])])
395-
396-
If you pass ``orient='index'``, the keys will be the row labels. But in this
397-
case you must also pass the desired column names:
398-
399-
.. ipython:: python
400-
401-
pd.DataFrame.from_items([('A', [1, 2, 3]), ('B', [4, 5, 6])],
402-
orient='index', columns=['one', 'two', 'three'])
403394
404395
Column selection, addition, deletion
405396
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -539,7 +530,7 @@ To write code compatible with all versions of Python, split the assignment in tw
539530
you'll need to take care when passing ``assign`` expressions that
540531

541532
* Updating an existing column
542-
* Refering to the newly updated column in the same ``assign``
533+
* Referring to the newly updated column in the same ``assign``
543534

544535
For example, we'll update column "A" and then refer to it when creating "B".
545536

doc/source/whatsnew/v0.14.1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Performance
145145
~~~~~~~~~~~
146146
- Improvements in dtype inference for numeric operations involving yielding performance gains for dtypes: ``int64``, ``timedelta64``, ``datetime64`` (:issue:`7223`)
147147
- Improvements in Series.transform for significant performance gains (:issue:`6496`)
148-
- Improvements in DataFrame.transform with ufuncs and built-in grouper functions for signifcant performance gains (:issue:`7383`)
148+
- Improvements in DataFrame.transform with ufuncs and built-in grouper functions for significant performance gains (:issue:`7383`)
149149
- Regression in groupby aggregation of datetime64 dtypes (:issue:`7555`)
150150
- Improvements in `MultiIndex.from_product` for large iterables (:issue:`7627`)
151151

pandas/_libs/groupby_helper.pxi.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def group_rank_{{name}}(ndarray[float64_t, ndim=2] out,
426426
labels : array containing unique label for each group, with its ordering
427427
matching up to the corresponding record in `values`
428428
is_datetimelike : bool
429-
unused in this method but provided for call compatability with other
429+
unused in this method but provided for call compatibility with other
430430
Cython transformations
431431
ties_method : {'keep', 'top', 'bottom'}
432432
* keep: leave NA values where they are

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def _from_inferred_categories(cls, inferred_categories, inferred_codes,
521521
cats = to_timedelta(inferred_categories, errors='coerce')
522522

523523
if known_categories:
524-
# recode from observation oder to dtype.categories order
524+
# recode from observation order to dtype.categories order
525525
categories = dtype.categories
526526
codes = _recode_for_categories(inferred_codes, cats, categories)
527527
elif not cats.is_monotonic_increasing:

pandas/core/frame.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,12 +1255,14 @@ def to_records(self, index=True, convert_datetime64=True):
12551255

12561256
@classmethod
12571257
def from_items(cls, items, columns=None, orient='columns'):
1258-
"""
1258+
"""Construct a dataframe from a list of tuples
1259+
12591260
.. deprecated:: 0.23.0
1260-
from_items is deprecated and will be removed in a
1261-
future version. Use :meth:`DataFrame.from_dict(dict())`
1262-
instead. :meth:`DataFrame.from_dict(OrderedDict(...))` may be used
1263-
to preserve the key order.
1261+
`from_items` is deprecated and will be removed in a future version.
1262+
Use :meth:`DataFrame.from_dict(dict(items)) <DataFrame.from_dict>`
1263+
instead.
1264+
:meth:`DataFrame.from_dict(OrderedDict(items)) <DataFrame.from_dict>`
1265+
may be used to preserve the key order.
12641266
12651267
Convert (key, value) pairs to DataFrame. The keys will be the axis
12661268
index (usually the columns, but depends on the specified
@@ -1284,8 +1286,8 @@ def from_items(cls, items, columns=None, orient='columns'):
12841286
"""
12851287

12861288
warnings.warn("from_items is deprecated. Please use "
1287-
"DataFrame.from_dict(dict()) instead. "
1288-
"DataFrame.from_dict(OrderedDict()) may be used to "
1289+
"DataFrame.from_dict(dict(items), ...) instead. "
1290+
"DataFrame.from_dict(OrderedDict(items)) may be used to "
12891291
"preserve the key order.",
12901292
FutureWarning, stacklevel=2)
12911293

pandas/core/indexes/datetimelike.py

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22
Base and utility classes for tseries type pandas objects.
33
"""
44
import warnings
5-
5+
import operator
66
from datetime import datetime, timedelta
77

88
from pandas import compat
99
from pandas.compat.numpy import function as nv
1010
from pandas.core.tools.timedeltas import to_timedelta
1111

1212
import numpy as np
13+
14+
from pandas._libs import lib, iNaT, NaT
15+
from pandas._libs.tslibs.period import Period
16+
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds
17+
from pandas._libs.tslibs.timestamps import round_ns
18+
1319
from pandas.core.dtypes.common import (
1420
_ensure_int64,
1521
is_dtype_equal,
@@ -25,18 +31,15 @@
2531
is_integer_dtype,
2632
is_object_dtype,
2733
is_string_dtype,
34+
is_period_dtype,
2835
is_timedelta64_dtype)
2936
from pandas.core.dtypes.generic import (
3037
ABCIndex, ABCSeries, ABCPeriodIndex, ABCIndexClass)
3138
from pandas.core.dtypes.missing import isna
3239
from pandas.core import common as com, algorithms, ops
3340
from pandas.core.algorithms import checked_add_with_arr
34-
from pandas.errors import NullFrequencyError
41+
from pandas.errors import NullFrequencyError, PerformanceWarning
3542
import pandas.io.formats.printing as printing
36-
from pandas._libs import lib, iNaT, NaT
37-
from pandas._libs.tslibs.period import Period
38-
from pandas._libs.tslibs.timedeltas import delta_to_nanoseconds
39-
from pandas._libs.tslibs.timestamps import round_ns
4043

4144
from pandas.core.indexes.base import Index, _index_shared_docs
4245
from pandas.util._decorators import Appender, cache_readonly
@@ -637,13 +640,33 @@ def _sub_datelike(self, other):
637640
def _sub_period(self, other):
638641
return NotImplemented
639642

640-
def _add_offset_array(self, other):
641-
# Array/Index of DateOffset objects
642-
return NotImplemented
643+
def _addsub_offset_array(self, other, op):
644+
"""
645+
Add or subtract array-like of DateOffset objects
643646
644-
def _sub_offset_array(self, other):
645-
# Array/Index of DateOffset objects
646-
return NotImplemented
647+
Parameters
648+
----------
649+
other : Index, np.ndarray
650+
object-dtype containing pd.DateOffset objects
651+
op : {operator.add, operator.sub}
652+
653+
Returns
654+
-------
655+
result : same class as self
656+
"""
657+
assert op in [operator.add, operator.sub]
658+
if len(other) == 1:
659+
return op(self, other[0])
660+
661+
warnings.warn("Adding/subtracting array of DateOffsets to "
662+
"{cls} not vectorized"
663+
.format(cls=type(self).__name__), PerformanceWarning)
664+
665+
res_values = op(self.astype('O').values, np.array(other))
666+
kwargs = {}
667+
if not is_period_dtype(self):
668+
kwargs['freq'] = 'infer'
669+
return self._constructor(res_values, **kwargs)
647670

648671
@classmethod
649672
def _add_datetimelike_methods(cls):
@@ -660,26 +683,31 @@ def __add__(self, other):
660683
other = lib.item_from_zerodim(other)
661684
if isinstance(other, ABCSeries):
662685
return NotImplemented
663-
elif is_timedelta64_dtype(other):
686+
687+
# scalar others
688+
elif isinstance(other, (DateOffset, timedelta, np.timedelta64)):
664689
result = self._add_delta(other)
665-
elif isinstance(other, (DateOffset, timedelta)):
690+
elif isinstance(other, (datetime, np.datetime64)):
691+
result = self._add_datelike(other)
692+
elif is_integer(other):
693+
# This check must come after the check for np.timedelta64
694+
# as is_integer returns True for these
695+
result = self.shift(other)
696+
697+
# array-like others
698+
elif is_timedelta64_dtype(other):
699+
# TimedeltaIndex, ndarray[timedelta64]
666700
result = self._add_delta(other)
667701
elif is_offsetlike(other):
668702
# Array/Index of DateOffset objects
669-
result = self._add_offset_array(other)
703+
result = self._addsub_offset_array(other, operator.add)
670704
elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
671705
if hasattr(other, '_add_delta'):
672706
# i.e. DatetimeIndex, TimedeltaIndex, or PeriodIndex
673707
result = other._add_delta(self)
674708
else:
675709
raise TypeError("cannot add TimedeltaIndex and {typ}"
676710
.format(typ=type(other)))
677-
elif is_integer(other):
678-
# This check must come after the check for timedelta64_dtype
679-
# or else it will incorrectly catch np.timedelta64 objects
680-
result = self.shift(other)
681-
elif isinstance(other, (datetime, np.datetime64)):
682-
result = self._add_datelike(other)
683711
elif isinstance(other, Index):
684712
result = self._add_datelike(other)
685713
elif is_integer_dtype(other) and self.freq is None:
@@ -709,28 +737,33 @@ def __sub__(self, other):
709737
other = lib.item_from_zerodim(other)
710738
if isinstance(other, ABCSeries):
711739
return NotImplemented
712-
elif is_timedelta64_dtype(other):
740+
741+
# scalar others
742+
elif isinstance(other, (DateOffset, timedelta, np.timedelta64)):
713743
result = self._add_delta(-other)
714-
elif isinstance(other, (DateOffset, timedelta)):
744+
elif isinstance(other, (datetime, np.datetime64)):
745+
result = self._sub_datelike(other)
746+
elif is_integer(other):
747+
# This check must come after the check for np.timedelta64
748+
# as is_integer returns True for these
749+
result = self.shift(-other)
750+
elif isinstance(other, Period):
751+
result = self._sub_period(other)
752+
753+
# array-like others
754+
elif is_timedelta64_dtype(other):
755+
# TimedeltaIndex, ndarray[timedelta64]
715756
result = self._add_delta(-other)
716757
elif is_offsetlike(other):
717758
# Array/Index of DateOffset objects
718-
result = self._sub_offset_array(other)
759+
result = self._addsub_offset_array(other, operator.sub)
719760
elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
720761
# We checked above for timedelta64_dtype(other) so this
721762
# must be invalid.
722763
raise TypeError("cannot subtract TimedeltaIndex and {typ}"
723764
.format(typ=type(other).__name__))
724765
elif isinstance(other, DatetimeIndex):
725766
result = self._sub_datelike(other)
726-
elif is_integer(other):
727-
# This check must come after the check for timedelta64_dtype
728-
# or else it will incorrectly catch np.timedelta64 objects
729-
result = self.shift(-other)
730-
elif isinstance(other, (datetime, np.datetime64)):
731-
result = self._sub_datelike(other)
732-
elif isinstance(other, Period):
733-
result = self._sub_period(other)
734767
elif isinstance(other, Index):
735768
raise TypeError("cannot subtract {typ1} and {typ2}"
736769
.format(typ1=type(self).__name__,

pandas/core/indexes/datetimes.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -964,29 +964,6 @@ def _add_offset(self, offset):
964964
"or DatetimeIndex", PerformanceWarning)
965965
return self.astype('O') + offset
966966

967-
def _add_offset_array(self, other):
968-
# Array/Index of DateOffset objects
969-
if len(other) == 1:
970-
return self + other[0]
971-
else:
972-
warnings.warn("Adding/subtracting array of DateOffsets to "
973-
"{} not vectorized".format(type(self)),
974-
PerformanceWarning)
975-
return self.astype('O') + np.array(other)
976-
# TODO: pass freq='infer' like we do in _sub_offset_array?
977-
# TODO: This works for __add__ but loses dtype in __sub__
978-
979-
def _sub_offset_array(self, other):
980-
# Array/Index of DateOffset objects
981-
if len(other) == 1:
982-
return self - other[0]
983-
else:
984-
warnings.warn("Adding/subtracting array of DateOffsets to "
985-
"{} not vectorized".format(type(self)),
986-
PerformanceWarning)
987-
res_values = self.astype('O').values - np.array(other)
988-
return self.__class__(res_values, freq='infer')
989-
990967
def _format_native_types(self, na_rep='NaT', date_format=None, **kwargs):
991968
from pandas.io.formats.format import _get_format_datetime64_from_values
992969
format = _get_format_datetime64_from_values(self, date_format)

pandas/core/indexes/period.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
from pandas.util._decorators import (Appender, Substitution, cache_readonly,
4545
deprecate_kwarg)
4646
from pandas.compat import zip, u
47-
from pandas.errors import PerformanceWarning
4847

4948
import pandas.core.indexes.base as ibase
5049
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
@@ -745,28 +744,6 @@ def _sub_period(self, other):
745744
# result must be Int64Index or Float64Index
746745
return Index(new_data)
747746

748-
def _add_offset_array(self, other):
749-
# Array/Index of DateOffset objects
750-
if len(other) == 1:
751-
return self + other[0]
752-
else:
753-
warnings.warn("Adding/subtracting array of DateOffsets to "
754-
"{cls} not vectorized"
755-
.format(cls=type(self).__name__), PerformanceWarning)
756-
res_values = self.astype('O').values + np.array(other)
757-
return self.__class__(res_values)
758-
759-
def _sub_offset_array(self, other):
760-
# Array/Index of DateOffset objects
761-
if len(other) == 1:
762-
return self - other[0]
763-
else:
764-
warnings.warn("Adding/subtracting array of DateOffsets to "
765-
"{cls} not vectorized"
766-
.format(cls=type(self).__name__), PerformanceWarning)
767-
res_values = self.astype('O').values - np.array(other)
768-
return self.__class__(res_values)
769-
770747
def shift(self, n):
771748
"""
772749
Specialized shift which produces an PeriodIndex

0 commit comments

Comments
 (0)