Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将DateTimeindex转换为仅包含年、小时和日,而不包含时间信息_Python_Pandas - Fatal编程技术网

Python 将DateTimeindex转换为仅包含年、小时和日,而不包含时间信息

Python 将DateTimeindex转换为仅包含年、小时和日,而不包含时间信息,python,pandas,Python,Pandas,我有一个数据帧,其索引也包含Hour:second:。。。但是我只希望它包含日期,如年、月、日 索引是否可能仍然是DateTimeIndex,但只包含年、月、日 当前索引如下所示: my_index = DatetimeIndex(['2017-08-25', '2017-08-24', '2017-08-23', '2017-08-22', '2017-08-21', '2017-08-20', '2017-08-19', '2017-08-18',

我有一个数据帧,其索引也包含Hour:second:。。。但是我只希望它包含日期,如年、月、日

索引是否可能仍然是DateTimeIndex,但只包含年、月、日

当前索引如下所示:

my_index = DatetimeIndex(['2017-08-25', '2017-08-24', '2017-08-23', '2017-08-22',
           '2017-08-21', '2017-08-20', '2017-08-19', '2017-08-18',
           '2017-08-17', '2017-08-16',
           ...
           '2015-07-19', '2015-07-18', '2015-07-17', '2015-07-16',
           '2015-07-15', '2015-07-14', '2015-07-13', '2015-07-12',
           '2015-07-11', '2015-07-10'],
          dtype='datetime64[ns]', length=778, freq=None)
我可以做到:

only_date_index = [el.date() for el in my_index]
但是如果我想在pandas中使用重采样功能,我会得到错误: TypeError:仅对DatetimeIndex、TimedeltaIndex或PeriodIndex有效,但获得了“Index”的实例。

您需要:

样本:

rng = pd.date_range('2017-04-03 15:00:45', periods=10, freq='24T')
df = pd.DataFrame({'a': range(10)}, index=rng)  
print (df)
                     a
2017-04-03 15:00:45  0
2017-04-03 15:24:45  1
2017-04-03 15:48:45  2
2017-04-03 16:12:45  3
2017-04-03 16:36:45  4
2017-04-03 17:00:45  5
2017-04-03 17:24:45  6
2017-04-03 17:48:45  7
2017-04-03 18:12:45  8
2017-04-03 18:36:45  9

myIndex = df.index.floor('D')
print (myIndex)
DatetimeIndex(['2017-04-03', '2017-04-03', '2017-04-03', '2017-04-03',
               '2017-04-03', '2017-04-03', '2017-04-03', '2017-04-03',
               '2017-04-03', '2017-04-03'],
              dtype='datetime64[ns]', freq=None)
感谢您提供另一个解决方案-使用:

计时

ix = pd.date_range('1970-01-01', '2200-01-15', freq='1H')

print (len(ix))
2016481

In [68]: %timeit (ix.normalize())
10 loops, best of 3: 178 ms per loop

In [69]: %timeit (ix.floor('d'))
10 loops, best of 3: 38.4 ms per loop

#solution from Dror (https://stackoverflow.com/questions/45954497/in-pandas-group-by-date-from-datetimeindex)
In [70]: %timeit pd.to_datetime(ix.date)
1 loop, best of 3: 5.09 s per loop
尝试
.normalize()
它将使时间变为午夜,因为我相信
DateTimeIndex
需要时间
。normalize()
也可以做到这一点,它是DateTimeIndex数组的嵌入式函数,但不是单个实例
myIndex = df.index.normalize()
print (myIndex)
DatetimeIndex(['2017-04-03', '2017-04-03', '2017-04-03', '2017-04-03',
               '2017-04-03', '2017-04-03', '2017-04-03', '2017-04-03',
               '2017-04-03', '2017-04-03'],
              dtype='datetime64[ns]', freq=None)
ix = pd.date_range('1970-01-01', '2200-01-15', freq='1H')

print (len(ix))
2016481

In [68]: %timeit (ix.normalize())
10 loops, best of 3: 178 ms per loop

In [69]: %timeit (ix.floor('d'))
10 loops, best of 3: 38.4 ms per loop

#solution from Dror (https://stackoverflow.com/questions/45954497/in-pandas-group-by-date-from-datetimeindex)
In [70]: %timeit pd.to_datetime(ix.date)
1 loop, best of 3: 5.09 s per loop