【问题标题】:How I can print the start and end dates of a data frame?如何打印数据框的开始日期和结束日期?
【发布时间】:2022-01-23 19:38:41
【问题描述】:

我有这些代码行,当我运行它时,我希望看到训练、验证和测试的日期,如下所示:

2012-01-01 00:00:00 --- 2013-12-31 23:00:00:00

而不是索引:

20150 --- 20189
datos = df_energy.loc['2015-07-02 01:00:00': '2021-11-30 15:00:00']

fin_train = '2019-12-31 23:59:00'
fin_validacion = '2020-11-30 23:59:00'

datos_train = datos.loc[:fin_train, :]
datos_val   = datos.loc[fin_train: fin_validacion, :]
datos_test  = datos.loc[fin_validacion:, :]

print(f"Fechas train      : {datos_train.index.min()} --- {datos_train.index.max()}")
print(f"Fechas validacion : {datos_val.index.min()} --- {datos_val.index.max()}")
print(f"Fechas test       : {datos_test.index.min()} --- {datos_test.index.max()}")

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    您已经有了索引,只需使用它们来获取相关值。我将为训练数据示例执行此操作,然后您可以将其平等地传输到其他数据集:

    def gen_timerange(df, col='UTC Time'):
        min_time = df.loc[df.index.min(), col]
        max_time = df.loc[df.index.max(), col]
        return f"{min_time} --- {max_time}"
    
    print(f"Fechas train      : {gen_timerange(datos_train)}")
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      相关资源
      最近更新 更多