Skip to content Skip to sidebar Skip to footer

Select Rows Between Two Datetimeindex Dates

I have a CSV file of the following format: vm,time,LoadInt1 abc-webapp-02,2017-05-31 10:00:00,3.133333 abc-webapp-02,2017-05-31 10:05:00,0.000000 abc-webapp-02,2017-05-31 10:10:00,

Solution 1:

Using query method:

df = pd.read_csv("my_file.csv", index_col=1, parse_dates=True)

In [121]: df.query("'2017-05-30' <= index <= '2017-06-01'")
Out[121]:
                                vm  LoadInt1
time2017-05-3110:00:00  abc-webapp-023.1333332017-05-3110:05:00  abc-webapp-020.0000002017-05-3110:10:00  abc-webapp-020.0000002017-05-3110:15:00  abc-webapp-020.0000002017-05-3110:20:00  abc-webapp-020.0000002017-05-3110:25:00  abc-webapp-020.0000002017-05-3110:30:00  abc-webapp-020.0000002017-05-3110:35:00  abc-webapp-020.0000002017-05-3110:40:00  abc-webapp-020.000000

Solution 2:

  • This type of index slicing includes the end points and so what you have will include the entire sample set

    df.loc['2017-05-30':'2017-05-31']#df['2017-05-30':'2017-05-31']vmLoadInt1time2017-05-31 10:00:00  abc-webapp-023.1333332017-05-31 10:05:00  abc-webapp-020.0000002017-05-31 10:10:00  abc-webapp-020.0000002017-05-31 10:15:00  abc-webapp-020.0000002017-05-31 10:20:00  abc-webapp-020.0000002017-05-31 10:25:00  abc-webapp-020.0000002017-05-31 10:30:00  abc-webapp-020.0000002017-05-31 10:35:00  abc-webapp-020.0000002017-05-31 10:40:00  abc-webapp-020.000000
  • This shows the same thing but actually subsets

    df.loc['2017-05-3110:10':'2017-05-3110:35']vmLoadInt1time2017-05-31 10:10:00  abc-webapp-020.02017-05-31 10:15:00  abc-webapp-020.02017-05-31 10:20:00  abc-webapp-020.02017-05-31 10:25:00  abc-webapp-020.02017-05-31 10:30:00  abc-webapp-020.02017-05-31 10:35:00  abc-webapp-020.0
  • Your import could be made smaller. You don't need the parser

    df = pd.read_csv("my_file.csv", parse_dates=[1], index_col=1)
    

Post a Comment for "Select Rows Between Two Datetimeindex Dates"