Pandas datetimeindex with numpy.maximum gives error

I am experiencing an error, which is probably an error in pandas (v. 0.22 on Windows, Python version 3.6.3) or, rather, in its interaction with NumPy (v. 1.14), but I am wondering if I missing something deeper. "

Here's the problem: if I have two objects Datetimeindexwith the same length and I use np.maximumbetween them, the output will be as expected:

import pandas as pd
import numpy as np
v1 = pd.DatetimeIndex(['2016-01-01', '2018-01-02', '2018-01-03'])
v2 = pd.DatetimeIndex(['2017-01-01', '2017-01-02', '2019-01-03'])
np.maximum(v1, v2)

returns the maximum element:

DatetimeIndex (['2017-01-01', '2018-01-02', '2019-01-03'], dtype = 'datetime64 [ns]', freq = None)

However, if I try to use only one of the two elements, I get an error message:

np.maximum(v1, v2[0])

pandas_libs \ tslib.pyx in pandas._ libs.tslib._Timestamp. richcmp ()

TypeError: "Timestamp" "int"

, , , - , pydatetime:

np.maximum(v1, v2[:1])

DatetimeIndex (['2017-01-01', '2018-01-02', '2018-01-03'], dtype = 'datetime64 [ns]', freq = None)

v1.to_pydatetime() - v2[0].to_pydatetime()

array ([datetime.datetime(2017, 1, 1, 0, 0),        datetime.datetime(2018, 1, 2, 0, 0),        datetime.datetime(2018, 1, 3, 0, 0)], dtype = object)

, v2 - v1[0] , v2 - v1[:1] (, , ).

+4
3

pd.Series, pd.Series.clip:

pd.Series(v1).clip(v2[0])

# 0   2017-01-01
# 1   2018-01-02
# 2   2018-01-03
# dtype: datetime64[ns]
+1
TypeError: Cannot compare type 'Timestamp' with type 'long'

, -

np.maximum(v1, v2[0])

? . . 2 , .

2- -

print(np.maximum(v1, v2[1:2]))
print(np.maximum(v1, v2[:1]))
print(np.maximum(v1, v2[2:3]))

, .

0

Documentation np.maximumexpected behavior. note that the operation v1[0]is called a choice and returns a single TimeStamp , and slicingv[:1] is known and returns an array.

here is part of the output help(np.maximum)

|  
 |  op(X, Y, out=None)
 |  Apply `op` to `X` and `Y` elementwise. May "broadcast" to make
 |  the shapes of `X` and `Y` congruent.
 |  
 |  The broadcasting rules are:
 |  
 |  * Dimensions of length 1 may be prepended to either array.
 |  * Arrays may be repeated along dimensions of length 1.
 |  
 |  Parameters
 |  ----------
 |  X : array_like
 |      First input array.
 |  Y : array_like
 |      Second input array.
 |  out : array_like
 |      An array to store the output. Must be the same shape as the
 |      output would have.
 |  
 |  Returns
 |  -------
 |  r : array_like
 |      The return value; if out is provided, `r` will be equal to out.
0
source

Source: https://habr.com/ru/post/1693518/


All Articles