Pandas 熊猫数据帧时间序列

Pandas 熊猫数据帧时间序列,pandas,time-series,Pandas,Time Series,我想构建一个以datetimestamp(最多分钟)为索引的数据框架,并在获取每个新列的数据时不断添加列。例如,对于Col-A,我按天、小时和分钟从另一个数据集聚合到一个值“k”。我想将这个值“k”插入“right”行索引处的数据帧中。我面临的问题是当前行标识符来自日期、小时、分钟上的groupby对象。不确定如何将这3个“连接”成一个好的timeseries类型 这是我当前拥有的(我的groupby对象的输出): 为了形成一个新的数据框来容纳许多列(a、B、…、Z),我将这样做: index

我想构建一个以datetimestamp(最多分钟)为索引的数据框架,并在获取每个新列的数据时不断添加列。例如,对于Col-A,我按天、小时和分钟从另一个数据集聚合到一个值“k”。我想将这个值“k”插入“right”行索引处的数据帧中。我面临的问题是当前行标识符来自日期、小时、分钟上的groupby对象。不确定如何将这3个“连接”成一个好的timeseries类型

这是我当前拥有的(我的groupby对象的输出):

为了形成一个新的数据框来容纳许多列(a、B、…、Z),我将这样做:

index = pd.date_range('2015-10-05 10:00:00', '2015-11-10 10:00:00', freq='1min')
df = pd.DataFrame(index=index)
现在,我想“以某种方式”获取值65并填充到我的数据帧中。我该怎么做?我必须以某种方式将“日期、小时、分钟”表单groupby对象转换为timeseries对象

此外,我将在当天的许多分钟内为Col-a提供一系列值。我想用这些值填充整个列,并用“0s”填充其余的列。然后,继续处理/填充下一列

我可以这样做吗:

str = '2015-10-10 06:10:00'

str
Out[362]: '2015-10-10 06:10:00'

pd.to_datetime(str, format='%Y-%m-%d %H:%M:%S', coerce=True)
Out[363]: Timestamp('2015-10-10 06:10:00')

row_idx = pd.to_datetime(str, format='%Y-%m-%d %H:%M:%S', coerce=True)

type(row_idx)
Out[365]: pandas.tslib.Timestamp

data = pd.DataFrame({'Col-A': 65}, index = pd.Series(row_idx))
df.add(data)

有什么想法吗?

您几乎在代码中找到了它。 几处改动就完成了

  • 在没有数据和时间索引的情况下初始化数据帧。(你 以后总是可以追加更多行)
  • 使用设置为0的值初始化新列
  • 在目标时间设置列的值
  • |

    str = '2015-10-10 06:10:00'
    
    str
    Out[362]: '2015-10-10 06:10:00'
    
    pd.to_datetime(str, format='%Y-%m-%d %H:%M:%S', coerce=True)
    Out[363]: Timestamp('2015-10-10 06:10:00')
    
    row_idx = pd.to_datetime(str, format='%Y-%m-%d %H:%M:%S', coerce=True)
    
    type(row_idx)
    Out[365]: pandas.tslib.Timestamp
    
    data = pd.DataFrame({'Col-A': 65}, index = pd.Series(row_idx))
    df.add(data)
    
    import pandas as pd
    
    
    index = pd.date_range('2015-10-05 10:00:00', '2015-11-10 10:00:00', freq='1min')
    df = pd.DataFrame(index=index)
    
    # initialize the column with all values set to 0. 
    df['first_column'] = 0
    # format the target time into a timestamp
    target_time = pd.to_datetime('2015-10-15 6:38')
    # set the value for the target time to 65
    df['first_column'][ target_time]=65
    # output the value at the target time. 
    df['first_column'][ target_time]