比较来自世界各地的卖家的域名和 IT 服务价格

怎么回事 Pandas `DatetimeIndex`?

我有
pandas.DatetimeIndex

, 例如:


pd.date_range/'2012-1-1 02:03:04.000',periods=3,freq='1ms'/
>>> [2012-01-01 02:03:04, ..., 2012-01-01 02:03:04.002000]


我想环游日期 /
Timestamp

和/ 到最近的第二个。 我该怎么做? 预期结果是类似的:


[2012-01-01 02:03:04.000000, ..., 2012-01-01 02:03:04.000000]


是可以做到的吗? Numpy
datetime64[ns]

长达秒钟而不变化
dtype


[ns]

?


np.array/['2012-01-02 00:00:00.001'],dtype='datetime64[ns]'/
已邀请:

莫问

赞同来自:

更新:如果您使用列进行 DatetimeIndex / datetime64, 最好使用
np.round

直接而不是通过 apply/map:


np.round/dtindex_or_datetime_col.astype/np.int64/, -9/.astype/'datetime64[ns]'/


旧答案 /与另一个解释/:

虽然答案 @Matti's 显然是应对你的情况的正确方法,我以为我会尽可能地添加答案 Timestamp 到最近的第二个:


from pandas.lib import Timestamp

t1 = Timestamp/'2012-1-1 00:00:00'/
t2 = Timestamp/'2012-1-1 00:00:00.000333'/

In [4]: t1
Out[4]: <timestamp: 00:00:00="" 2012-01-01="">

In [5]: t2
Out[5]: <timestamp: 00:00:00.000333="" 2012-01-01="">

In [6]: t2.microsecond
Out[6]: 333

In [7]: t1.value
Out[7]: 1325376000000000000L

In [8]: t2.value
Out[8]: 1325376000000333000L

# Alternatively: t2.value - t2.value % 1000000000
In [9]: long/round/t2.value, -9// # round milli-, micro- and nano-seconds
Out[9]: 1325376000000000000L

In [10]: Timestamp/long/round/t2.value, -9///
Out[10]: <timestamp: 00:00:00="" 2012-01-01="">


因此,您可以将其应用于整个索引:


def to_the_second/ts/:
return Timestamp/long/round/ts.value, -9///

dtindex.map/to_the_second/


</timestamp:></timestamp:></timestamp:>

二哥

赞同来自:

方法
round//

被添加为 DatetimeIndex, Timestamp, TimedeltaIndex 和 Timedelta 在 pandas 0.18.0. 现在我们可以执行以下操作:


In[114]: index = pd.DatetimeIndex/[pd.Timestamp/'2012-01-01 02:03:04.000'/, pd.Timestamp/'2012-01-01 02:03:04.002'/, pd.Timestamp/'20130712 02:03:04.500'/, pd.Timestamp/'2012-01-01 02:03:04.501'/]/

In[115]: index.values
Out[115]:
array/['2012-01-01T02:03:04.000000000', '2012-01-01T02:03:04.002000000',
'2013-07-12T02:03:04.500000000', '2012-01-01T02:03:04.501000000'], dtype='datetime64[ns]'/

In[116]: index.round/'S'/
Out[116]:
DatetimeIndex/['2012-01-01 02:03:04', '2012-01-01 02:03:04',
'2013-07-12 02:03:04', '2012-01-01 02:03:05'],
dtype='datetime64[ns]', freq=None/



round//

采用频率参数。 字符串伪名为他列出
http://pandas.pydata.org/panda ... iases
.

窦买办

赞同来自:

改变索引本身没有特别的意义 - 因为你可以简单地生成它
date_range

具有所需的频率参数,如您的问题所示。

我假设您正在尝试更改包含数据的时间序列的频率,在这种情况下可以使用
resample

/
http://pandas.pydata.org/panda ... pling
/., 例如,如果您有以下时间序列:


dt_index = pd.date_range/'2012-1-1 00:00.001',periods=3, freq='1ms'/
ts = pd.Series/randn/3/, index=dt_index/


2012-01-01 00:00:00 0.594618
2012-01-01 00:00:00.001000 0.874552
2012-01-01 00:00:00.002000 -0.700076
Freq: L


然后,您可以将频率更改为第二个使用 resample, 指定您希望如何聚合值 /平均值,总和等/:


ts.resample/'S', how='sum'/

2012-01-01 00:00:00 0.594618
2012-01-01 00:00:01 0.174475
Freq: S

八刀丁二

赞同来自:

对于更普遍的舍入,您可以使用对象的事实 Pandas
Timestamp

主要使用标准库
datetime.datetime

API, 包括方法
datetime.datetime.replace//

.

所以,为了解决你的圆形微秒的问题,你可以做到:


import datetime
import pandas as pd

times = pd.date_range/'2012-1-1 02:03:04.499',periods=3,freq='1ms'/
# Add 5e5 microseconds and truncate to simulate rounding
times_rounded = [/x + datetime.timedelta/microseconds=5e5//.replace/microsecond=0/ for x in times]

from IPython.display import display
print/'Before:'/
display/list/times//
print/'After:'/
display/list/times_rounded//


输出:


Before:
[Timestamp/'2012-01-01 02:03:04.499000', offset='L'/,
Timestamp/'2012-01-01 02:03:04.500000', offset='L'/,
Timestamp/'2012-01-01 02:03:04.501000', offset='L'/]
After:
[Timestamp/'2012-01-01 02:03:04', offset='L'/,
Timestamp/'2012-01-01 02:03:05', offset='L'/,
Timestamp/'2012-01-01 02:03:05', offset='L'/]


您可以使用相同的技术,例如,舍入到不久的将来。 /如果你不打扰闰秒等/:


times = pd.date_range/'2012-1-1 08:00:00', periods=3, freq='4H'/
times_rounded = [/x + datetime.timedelta/hours=12//.replace/hour=0, second=0, microsecond=0/ for x in times]


受到这篇文章的启发。 SO:

要回复问题请先登录注册