Home>
I want to extract from time A to time B from Datetime type object

I'm new to Python. As the title suggests, we want to specify the time% M:% H:% D from the Datetime type object so that the range can be extracted.

How far have you done?
import pandas as pd
dataframe ["TimeStamp"] = pd.to_datetime (dataframe ["TimeStamp"])
# You ’ve done so far

If i am good at Python, I hope you can teach it.

  • Answer # 1

    Is it better to use a slice with DatetimeIndex (TimeStamp column set to Index)

    import pandas as pd
    dataframe = pd.DataFrame ({
        'TimeStamp': ['2018/01/01 12:00',
                       '2018/01/01 18:00',
                       '2018/01/02 00:00',
                       '2018/01/02 06:00',
                       '2018/01/02 12:00',
                       '2018/01/02 18:00'],
        'Value': [1,2,3,4,5,6]
    })
    dataframe ["TimeStamp"] = pd.to_datetime (dataframe ["TimeStamp"])
    dataframe = dataframe.set_index ('TimeStamp')
    print (dataframe ['2018/01/01 20:00': '2018/01/02 08:00'])
    # Value
    #TimeStamp
    # 2018-01-02 00:00:00 3
    # 2018-01-02 06:00:00 4