【问题标题】:Select the data from between two timestamp in python从python中的两个时间戳之间选择数据
【发布时间】:2017-06-15 10:10:07
【问题描述】:

我的查询正在重新获取数据,给定 python 中的两个时间戳。

我需要一个输入字段,我可以在其中输入两个时间戳,然后从 CSV 读取中,我需要检索该特定输入。

实际数据(CSV)

      Daily_KWH_System  PowerScout  Temperature         Timestamp  Visibility    Daily_electric_cost   kW_System 
 0         4136.900384  P371602077           0  07/09/2016 23:58          0    180.657705            162.224216 
 1         3061.657187  P371602077          66  08/09/2016 23:59         10    133.693074            174.193804 
 2         4099.614033  P371602077          63  09/09/2016 05:58         10    179.029562            162.774013  
3          3922.490275  P371602077          63  10/09/2016 11:58         10    171.297701            169.230047  
4          3957.128982  P371602077          88  11/09/2016 17:58         10    172.806125            164.099307 

例子:

Input:
start date : 2-1-2017
end date :10-1-2017

输出

Timestamp     Value
2-1-2017      10
3-1-2017      35
.
.
.
.
10-1-2017     25

原始 CSV 将包含所有数据

Timestamp        Value
1-12-2016        10
2-12-2016        25
.
.
.
1-1-2017         15
2-1-2017         10
.
.
.
10-1-2017        25
.
.
31-1-2017        50

【问题讨论】:

    标签: python-3.x pandas datetime dataframe timestamp


    【解决方案1】:

    使用pd.read_csv读取文件

    df = pd.read_csv('my.csv', index_col='Timestamp', parse_dates=[0])
    

    然后使用您的输入进行切片

    df[start_date:end_date]
    

    【讨论】:

    • 时间戳格式为“26/09/2016 23:58”。因此它抛出一个Keyerror。 KeyError:时间戳('2016-12-02 00:00:00')
    【解决方案2】:

    如果所有开始和结束日期都在df.index 中,您似乎需要read_csv 中的dayfirst=True[] 选择:

    import pandas as pd
    from pandas.compat import StringIO
    
    temp=u"""Timestamp;Value
    1-12-2016;10
    2-12-2016;25
    1-1-2017;15
    2-1-2017;10
    10-1-2017;25
    31-1-2017;50"""
    #after testing replace 'StringIO(temp)' to 'filename.csv'
    #if necessary add sep
    #index_col=[0] convert first column to index
    #parse_dates=[0] parse first column to datetime
    df = pd.read_csv(StringIO(temp), sep=";", index_col=[0], parse_dates=[0], dayfirst=True)
    print (df)
                Value
    Timestamp        
    2016-12-01     10
    2016-12-02     25
    2017-01-01     15
    2017-01-02     10
    2017-01-10     25
    2017-01-31     50
    
    print (df.index.dtype)
    datetime64[ns]
    
    print (df.index)
    DatetimeIndex(['2016-12-01', '2016-12-02', '2017-01-01', '2017-01-02',
                   '2017-01-10', '2017-01-31'],
                  dtype='datetime64[ns]', name='Timestamp', freq=None)
    

    start_date = pd.to_datetime('2-1-2017', dayfirst=True)
    end_date  = pd.to_datetime('10-1-2017', dayfirst=True)
    print (df[start_date:end_date])
                Value
    Timestamp        
    2017-01-02     10
    2017-01-10     25
    

    如果某些日期不在索引中,您需要boolean indexing

    start_date = pd.to_datetime('3-1-2017', dayfirst=True)
    end_date  = pd.to_datetime('10-1-2017', dayfirst=True)
    
    print (df[(df.index > start_date) & (df.index > end_date)])
                Value
    Timestamp        
    2017-01-31     50
    

    【讨论】:

    • 时间戳格式为“26/09/2016 23:58”。因此它抛出一个Keyerror。 KeyError:时间戳('2016-12-02 00:00:00')
    • print (df[start_date:end_date])print (df[(df.index > start_date) & (df.index > end_date)]) ?
    • 如果第一个和数据在索引中,请尝试print (df[str(start_date):str(end_date)])
    • 好的,返回什么print (df.index.dtype)
    • 是的,我正在尝试更改它的日期类型。由于它抛出错误“无法将类型'Timestamp'与'str'类型进行比较”打印(df.index.dtype)=这是说df是一个对象
    猜你喜欢
    • 2018-04-17
    • 2012-02-02
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多