简体   繁体   中英

In Pandas, how to find row using exactly date

I have an dataframe, with date as index, I have an date object, I am trying to locate the row with that exact date, this is what I have:

candle.loc[candle.index == targetdate]

This is my date object:

datetime.datetime(2020, 12, 31, 8, 0, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))

This is my dataframe:

                  Open        High         Low       Close   Adj Close    Volume
Date                                                                            
2020-12-31  371.779999  374.660004  371.230011  373.880005  372.659454  78520700

When I try to run the code it return me an empty frame but you can see the date is in the dataframe, how do I get it?

Get date part from datetime and convert to string:

>>> candle.loc[candle.index == str(targetdate.date())]

                  Open        High         Low       Close   Adj Close    Volume
Date
2020-12-31  371.779999  374.660004  371.230011  373.880005  372.659454  78520700

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM