How to access pandas DataFrame datetime index using strings

后端 未结 3 938
轻奢々
轻奢々 2020-12-24 06:31

This is a very simple and practical question. I have the feeling that it must be a silly detail and that there should be similar questions. I wasn\'t able to find them tho.

3条回答
  •  情深已故
    2020-12-24 07:28

    pandas is taking what's inside the [] and deciding what it should do. If it's a subset of column names, it'll return a DataFrame with those columns. If it's a range of index values, it'll return a subset of those rows. What is does not handle is taking a single index value.

    Solution

    Two work around's

    1.Turn the argument into something pandas interprets as a range.

    df['2008-01-01':'2008-01-01']
    

    2.Use the method designed to give you this result. loc[]

    df.loc['2008-01-01']
    

    Link to the documentation

提交回复
热议问题