以下是我的数据框,其中包含多个Excel文件的值.我想做一个时间序列分析,所以我把索引作为datetimeindex.但我的索引没有根据日期安排.以下是我的数据框:
Item Details Unit Op. Qty Price Op. Amt. Cl. Qty Price.1 Cl. Amt.
Month
2013-04-01 5 In 1 Pcs -56.0 172.78 -9675.58 -68.0 175.79 -11953.96
2013-04-01 Adaptor Pcs -17.0 9.00 -152.99 -17.0 9.00 -152.99
2013-04-01 Agro Tape Pcs -2.0 26.25 -52.50 -2.0 26.25 -52.50
...
2014-01-01 12" Angal Pcs -6.0 31.50 -189.00 -6.0 31.50 -189.00
2014-01-01 13 Mm Electrical Drill Check Set -1.0 247.50 -247.50 -1.0 247.50 -247.50
2014-01-01 14" Blad Pcs -5.0 157.49 -787.45 -5.0 157.49 …Run Code Online (Sandbox Code Playgroud) 考虑一下 DateTimeIndex dates
dates = pd.date_range('2016-01-29', periods=4, freq='BM')
dates
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29'],
dtype='datetime64[ns]', freq='BM')
Run Code Online (Sandbox Code Playgroud)
我想在附加到对象的频率上将索引扩展一个句点.
我预计
pd.date_range('2016-01-29', periods=5, freq='BM')
DatetimeIndex(['2016-01-29', '2016-02-29', '2016-03-31', '2016-04-29',
'2016-05-31'],
dtype='datetime64[ns]', freq='BM')
Run Code Online (Sandbox Code Playgroud)
我试过了
dates.append(dates[[-1]] + pd.offsets.BusinessMonthEnd())
Run Code Online (Sandbox Code Playgroud)
然而
datesPerformanceWarning:应用于Series或DatetimeIndex的非向量化DateOffset
我的目标是有一个时间列表(以秒为单位),已经在一整天的5分钟内列入时间列表.这是我的代码,将"2016-07-08"的整天打包5分钟:
pd.date_range('2016-07-08 00:00:00', '2016-07-08 23:59:00', freq='5Min')
Run Code Online (Sandbox Code Playgroud)
结果 :
DatetimeIndex(['2016-07-08 00:00:00', '2016-07-08 00:05:00',
'2016-07-08 00:10:00', '2016-07-08 00:15:00',
'2016-07-08 00:20:00', '2016-07-08 00:25:00',
'2016-07-08 00:30:00', '2016-07-08 00:35:00',
'2016-07-08 00:40:00', '2016-07-08 00:45:00',
...
'2016-07-08 23:10:00', '2016-07-08 23:15:00',
'2016-07-08 23:20:00', '2016-07-08 23:25:00',
'2016-07-08 23:30:00', '2016-07-08 23:35:00',
'2016-07-08 23:40:00', '2016-07-08 23:45:00',
'2016-07-08 23:50:00', '2016-07-08 23:55:00'],
dtype='datetime64[ns]', length=288, freq='5T')
Run Code Online (Sandbox Code Playgroud)
这是每5分钟包含所有时间(按秒)的代码:
for time in pd.date_range('2016-07-08 00:00:00', '2016-07-08 23:59:00', freq='5Min').tolist():
time_by_5_min = datetime.datetime.strftime(time.to_datetime(), "%Y-%m-%d %H:%M:%S")
print pd.date_range(time_by_5_min, freq='S', periods=60)
Run Code Online (Sandbox Code Playgroud)
结果 :
DatetimeIndex(['2016-07-08 00:00:00', '2016-07-08 00:00:01',
'2016-07-08 00:00:02', '2016-07-08 00:00:03', …Run Code Online (Sandbox Code Playgroud) 我试图在我的数据框中为时间戳 HOUR、DAY、MONTH 和 DatetimeIndex 的值构建 3 个单独的列。
我为无法复制的数据道歉,因为我的数据集是从 CSV 文件中读取的。
boilerDf = pd.read_csv('C:\\Users\\Python Scripts\\Deltadata.csv', index_col='Date', parse_dates=True)
print(boilerDf.info())
Run Code Online (Sandbox Code Playgroud)
这将返回:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 23797 entries, 2017-10-25 05:00:08.436000 to 2018-01-02 05:45:14.419000
Data columns (total 3 columns):
hwr 23797 non-null float64
hws 23797 non-null float64
oat 23797 non-null float64
dtypes: float64(3)
memory usage: 743.7 KB
None
Run Code Online (Sandbox Code Playgroud)
我可以在 pandas.pydata.org 网站上看到,除了我想创建单独的数据框(列)之外,他们有 3 种方法可以用于我尝试做的事情:
DatetimeIndex.month
DatetimeIndex.day
DatetimeIndex.hour
Run Code Online (Sandbox Code Playgroud)
下面的代码不适用于为日期时间索引的小时添加单独的数据框列......有任何想法吗?
boilerDf['Hour'] = boilerDf.DatetimeIndex.hour
Run Code Online (Sandbox Code Playgroud)
亲切的问候
我也在 Github 上上传了数据: bbartling/Data on Github
有些数据每 5 秒收集一次,有时会丢失。
将它们加载到 Pandas 数据帧后,我想定义一个时间起点并准确提取 180 行(15 分钟 x 每分钟 12 个样本),无论起点如何。这些数据提供了一个绘图,并且始终保持相同的大小可以大大简化其余代码。
缺失的数据应该用 None 填充。
我认为有一些我不知道的快捷方式可以做到这一点:
import pandas as pd
import datetime
dt = [
"2018-02-08 13:45:05",
"2018-02-08 13:45:10",
"2018-02-08 13:45:25",
"2018-02-08 13:45:30",
"2018-02-08 13:45:35",
"2018-02-08 13:45:40",
"2018-02-08 13:45:50",
"2018-02-08 13:45:55",
"2018-02-08 13:46:00",
"2018-02-08 13:46:05",
]
wl = [
4737.25,
4834.80,
4885.53,
5003.98,
5031.08,
5215.90,
5147.65,
5100.50,
5038.94,
5020.67,
]
df = pd.DataFrame({"dt":dt, "wl":wl}).set_index("dt")
df.index = pd.to_datetime(df.index)
df = df.resample("5s").mean()
print(df)
Run Code Online (Sandbox Code Playgroud)
返回:
wl
dt
2018-02-08 …Run Code Online (Sandbox Code Playgroud) 我想合并两个由time和索引的数据集id.问题是,每个数据集的时间略有不同.在一个数据集中,时间(每月)是月中,因此是每个月的第15个月.在其他数据集中,这是最后一个工作日.这应该仍然是一对一的匹配,但日期并不完全相同.
我的方法是将月中日期转换为工作日的月末日期.
数据:
dt = pd.date_range('1/1/2011','12/31/2011', freq='D')
dt = dt[dt.day == 15]
lst = [1,2,3]
idx = pd.MultiIndex.from_product([dt,lst],names=['date','id'])
df = pd.DataFrame(np.random.randn(len(idx)), index=idx)
df.head()
Run Code Online (Sandbox Code Playgroud)
输出:
0
date id
2011-01-15 1 -0.598584
2 -0.484455
3 -2.044912
2011-02-15 1 -0.017512
2 0.852843
Run Code Online (Sandbox Code Playgroud)
这就是我想要的(我删除了性能警告):
In[83]:df.index.levels[0] + BMonthEnd()
Out[83]:
DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31', '2011-04-29',
'2011-05-31', '2011-06-30', '2011-07-29', '2011-08-31',
'2011-09-30', '2011-10-31', '2011-11-30', '2011-12-30'],
dtype='datetime64[ns]', freq='BM')
Run Code Online (Sandbox Code Playgroud)
但是,索引是不可变的,因此这不起作用:
In: df.index.levels[0] = df.index.levels[0] + BMonthEnd()
TypeError: 'FrozenList' does not support mutable operations.
Run Code Online (Sandbox Code Playgroud)
我得到的唯一解决方案是reset_index(),更改日期,然后再次set_index():
df.reset_index(inplace=True)
df['date'] = …Run Code Online (Sandbox Code Playgroud) 我有两个熊猫数据帧new_hpm和new_mr日期时间索引,我试图根据另一个使用的日期时间索引对一个数据帧进行子集化.loc。
两个数据帧的日期时间索引是:
new_hpm.index
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
'2013-01-05', '2013-01-06', '2013-01-07', '2013-01-08',
'2013-01-09', '2013-01-10',
...
'2017-12-15', '2017-12-20', '2017-12-21', '2017-12-22',
'2017-12-23', '2017-12-24', '2017-12-28', '2017-12-29',
'2017-12-30', '2017-12-31'],
dtype='datetime64[ns]', name='datetime', length=1093, freq=None)
new_mr.index
DatetimeIndex(['2013-01-07', '2013-01-07', '2013-01-13', '2013-01-13',
'2013-01-13', '2013-01-13', '2013-01-14', '2013-01-14',
'2013-01-14', '2013-01-14',
...
'2017-12-31', '2017-12-31', '2017-12-31', '2017-12-31',
'2017-12-31', '2017-12-31', '2017-12-31', '2017-12-31',
'2017-12-31', '2017-12-31'],
dtype='datetime64[ns]', name='date_conv', length=13366, freq=None)
Run Code Online (Sandbox Code Playgroud)
然而,当我这样做时
subset_mr = new_mr.loc[new_hpm.index]
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
ValueError: mixed datetimes and integers in passed array
Run Code Online (Sandbox Code Playgroud) 我有两个日期时间索引的数据框。一个缺少一些这样的日期时间 ( df1) 而另一个是完整的(具有常规时间戳,在本系列中没有任何间隙)并且充满了NaN's ( df2)。
我试图从DF1值匹配的指数df2,带馅NaN的,其中这种datetimeindex不存在的df1。
例子:
In [51]: df1
Out [51]: value
2015-01-01 14:00:00 20
2015-01-01 15:00:00 29
2015-01-01 16:00:00 41
2015-01-01 17:00:00 43
2015-01-01 18:00:00 26
2015-01-01 19:00:00 20
2015-01-01 20:00:00 31
2015-01-01 21:00:00 35
2015-01-01 22:00:00 39
2015-01-01 23:00:00 17
2015-03-01 00:00:00 6
2015-03-01 01:00:00 37
2015-03-01 02:00:00 56
2015-03-01 03:00:00 12
2015-03-01 04:00:00 41
2015-03-01 05:00:00 31
... ...
2018-12-25 23:00:00 41
<34843 …Run Code Online (Sandbox Code Playgroud) 我有一个名为“分数”的系列,带有日期时间索引。
最后,我要到其子集quarter和year
伪代码:series.loc['q2 of 2013']
迄今为止的尝试:
s.dt.quarter
AttributeError:只能使用具有类似日期时间的值的 .dt 访问器
s.index.dt.quarter
AttributeError: 'DatetimeIndex' 对象没有属性 'dt'
这有效(受此答案启发),但我无法相信这是在 Pandas 中执行此操作的正确方法:
d = pd.DataFrame(s)
d['date'] = pd.to_datetime(d.index)
d.loc[(d['date'].dt.quarter == 2) & (d['date'].dt.year == 2013)]['scores']
我希望有一种方法可以做到这一点,而无需转换为数据集,将索引强制为日期时间,然后从中获取系列。
我错过了什么,在 Pandas 系列上做到这一点的优雅方式是什么?
我有一个日期不完整的 DataFrame,我只需要每个月最后一天可用的日期/行。我尝试使用 TimeGrouper 并获取.last()每个组。
import pandas as pd
idx = [pd.datetime(2016,2,1),pd.datetime(2017,1,20),pd.datetime(2017,2,1),pd.datetime(2017,2,27)]
df = pd.DataFrame([1,2,3,4],index=idx)
df
0
2016-02-01 1
2017-01-20 2
2017-02-01 3
2017-02-27 4
Run Code Online (Sandbox Code Playgroud)
期待:
df_eom
0
2016-02-01 1
2017-01-20 2
2017-02-27 4
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个:
df_eom = df.groupby(pd.TimeGrouper(freq='1M')).last()
df_eom
0
2016-02-29 1.0
2016-03-31 NaN
2016-04-30 NaN
2016-05-31 NaN
2016-06-30 NaN
2016-07-31 NaN
2016-08-31 NaN
2016-09-30 NaN
2016-10-31 NaN
2016-11-30 NaN
2016-12-31 NaN
2017-01-31 2.0
2017-02-28 4.0
Run Code Online (Sandbox Code Playgroud)
它不仅创建了不在 df 中的日期,而且还更改了 df 第一行和最后一行的索引。我使用 TimeGrouper 错了吗?
datetimeindex ×10
pandas ×10
python ×8
datetime ×3
date ×1
date-range ×1
gaps-in-data ×1
list ×1
merge ×1
multi-index ×1
python-3.x ×1
reindex ×1
sorting ×1