【问题标题】:Truncating before/after a specific date and time Error在特定日期和时间之前/之后截断错误
【发布时间】:2019-04-22 04:07:50
【问题描述】:

我编写了一个函数,旨在在给定范围之前或之后截断我的数据。我传入一组日期,位置 1 是开始日期,位置 2 是结束日期。

我如何还可以选择指定时间和日期来截断 b4 及之后 - 我将如何修改我的代码来做到这一点?*)

另外 - 我的数据不断收到错误消息:

    raise InvalidIndexError('Reindexing only valid with uniquely'
pandas.core.indexes.base.InvalidIndexError: Reindexing only valid with uniquely valued Index
objects

我用:

date_range = self.data.copy()
nearest_start = date_range.iloc[
            date_range.index.get_loc(datetime.datetime.strptime(split[0], '%m-%d-%Y'),
                     method='nearest')].name
...
date_range = date_range.truncate(before=nearest_start, after=nearest_end)

我的日期元组:

('12-29-2011','12-23-2017')

这是我的数据集的 df.head():

                               Open       H   ...                RBE1min        O
        DateTime                              ...                                
        2007-11-06 12:45:00 -0.6437 -0.6423   ...     11/6/2007 12:45.00  21198.0
        2007-11-06 14:30:00 -0.6430 -0.6425   ...     11/6/2007 14:30.00  21355.0
        2007-11-06 19:33:00 -0.6423 -0.6400   ...     11/6/2007 19:33.00  21430.0
        2007-11-07 09:00:00 -0.6434 -0.6421   ...     11/7/2007 09:00.00  21440.0
        2007-11-07 12:26:00 -0.6433 -0.6396   ...     11/7/2007 12:26.00  21392.0

我已经运行以下命令来检查索引中的重复项并返回 NAT,所以我不确定为什么我仍然会收到此错误

任何帮助将不胜感激

dup = rbs4181.index.get_duplicates()

【问题讨论】:

标签: python python-3.x pandas numpy dataframe


【解决方案1】:

恐怕不是你想要的。但这可能会有所帮助:

我使用这个数据集http://archive.ics.uci.edu/ml/datasets/Occupancy+Detection+# 进行测试。

对于日期时间索引,解析为时间戳的日期和字符串可以作为索引参数传递,也可以作为 truncatepd.Timestampbeforeafter 参数传递。

In [1]: import pandas as pd

In [2]: df = pd.read_csv('datatest2.txt', parse_dates=[1], index_col=[1])

In [3]: df.index
Out[3]:
DatetimeIndex(['2015-02-11 14:48:00', '2015-02-11 14:49:00',
               '2015-02-11 14:50:00', '2015-02-11 14:51:00',
               '2015-02-11 14:51:59', '2015-02-11 14:53:00',
               '2015-02-11 14:54:00', '2015-02-11 14:55:00',
               '2015-02-11 14:55:59', '2015-02-11 14:57:00',
               ...
               '2015-02-18 09:10:00', '2015-02-18 09:10:59',
               '2015-02-18 09:11:59', '2015-02-18 09:13:00',
               '2015-02-18 09:14:00', '2015-02-18 09:15:00',
               '2015-02-18 09:16:00', '2015-02-18 09:16:59',
               '2015-02-18 09:17:59', '2015-02-18 09:19:00'],
              dtype='datetime64[ns]', name='date', length=9752, freq=None)

In [4]: df['2015-02-12':'2015-02-13'].index
Out[4]:
DatetimeIndex(['2015-02-12 00:00:00', '2015-02-12 00:01:00',
               '2015-02-12 00:02:00', '2015-02-12 00:03:00',
               '2015-02-12 00:04:00', '2015-02-12 00:04:59',
               '2015-02-12 00:06:00', '2015-02-12 00:07:00',
               '2015-02-12 00:08:00', '2015-02-12 00:08:59',
               ...
               '2015-02-13 23:50:00', '2015-02-13 23:51:00',
               '2015-02-13 23:51:59', '2015-02-13 23:53:00',
               '2015-02-13 23:54:00', '2015-02-13 23:55:00',
               '2015-02-13 23:55:59', '2015-02-13 23:57:00',
               '2015-02-13 23:57:59', '2015-02-13 23:58:59'],
              dtype='datetime64[ns]', name='date', length=2880, freq=None)

In [5]: df.truncate(before=pd.Timestamp('2015-02-12'), after=pd.Timestamp('2015-02-14')).index
Out[5]:
DatetimeIndex(['2015-02-12 00:00:00', '2015-02-12 00:01:00',
               '2015-02-12 00:02:00', '2015-02-12 00:03:00',
               '2015-02-12 00:04:00', '2015-02-12 00:04:59',
               '2015-02-12 00:06:00', '2015-02-12 00:07:00',
               '2015-02-12 00:08:00', '2015-02-12 00:08:59',
               ...
               '2015-02-13 23:51:00', '2015-02-13 23:51:59',
               '2015-02-13 23:53:00', '2015-02-13 23:54:00',
               '2015-02-13 23:55:00', '2015-02-13 23:55:59',
               '2015-02-13 23:57:00', '2015-02-13 23:57:59',
               '2015-02-13 23:58:59', '2015-02-14 00:00:00'],
              dtype='datetime64[ns]', name='date', length=2881, freq=None)

In [6]: df.truncate(before='2015-02-12', after='2015-02-14').index
Out[6]:
DatetimeIndex(['2015-02-12 00:00:00', '2015-02-12 00:01:00',
               '2015-02-12 00:02:00', '2015-02-12 00:03:00',
               '2015-02-12 00:04:00', '2015-02-12 00:04:59',
               '2015-02-12 00:06:00', '2015-02-12 00:07:00',
               '2015-02-12 00:08:00', '2015-02-12 00:08:59',
               ...
               '2015-02-13 23:51:00', '2015-02-13 23:51:59',
               '2015-02-13 23:53:00', '2015-02-13 23:54:00',
               '2015-02-13 23:55:00', '2015-02-13 23:55:59',
               '2015-02-13 23:57:00', '2015-02-13 23:57:59',
               '2015-02-13 23:58:59', '2015-02-14 00:00:00'],
              dtype='datetime64[ns]', name='date', length=2881, freq=None)

In [7]: df.truncate(before='2015-02-12 01:00:00', after='2015-02-13 23:00:00').index
Out[7]:
DatetimeIndex(['2015-02-12 01:00:00', '2015-02-12 01:01:00',
               '2015-02-12 01:01:59', '2015-02-12 01:02:59',
               '2015-02-12 01:04:00', '2015-02-12 01:05:00',
               '2015-02-12 01:06:00', '2015-02-12 01:07:00',
               '2015-02-12 01:08:00', '2015-02-12 01:08:59',
               ...
               '2015-02-13 22:51:00', '2015-02-13 22:52:00',
               '2015-02-13 22:53:00', '2015-02-13 22:53:59',
               '2015-02-13 22:54:59', '2015-02-13 22:56:00',
               '2015-02-13 22:57:00', '2015-02-13 22:58:00',
               '2015-02-13 22:59:00', '2015-02-13 22:59:59'],
              dtype='datetime64[ns]', name='date', length=2761, freq=None)

因此我认为您只需要修改您的函数来验证是否通过了有效日期(和时间)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2021-07-19
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    相关资源
    最近更新 更多