0

I have a dataframe with multiple dates. (2021-04-20, 2021-04-21, etc.)
I want to filter it on a specific date. (2021-04-21)

This is my code:

def select_by_date(df: pd.DataFrame, date: str = None):
    if date:
        log.info(f"Using data from {date}")
        df["datum"] = pd.to_datetime(df['datum'])
        return df.loc[(df["datum"] == date)]
    else:
        log.warn(f"No date provided")
        return input_sendungen

This is the dataframe before (pd.to_datetime): enter image description here

Dtype (Object):
enter image description here

This is the dataframe after (pd.to_datetime):

enter image description here

Dtype of the column is (df.info()):

enter image description here

Why is the returned dataframe empty?

daniel guo
  • 45
  • 1
  • 7
  • `date` is a string, so not equal to any `datetime`? Another question: Why do you convert the `datum` column to `datatime` (`df["datum"] = pd.to_datetime(df['datum'])`) when it already is `datetime` according to the screenshot? – Timus Jul 11 '22 at 20:20
  • I've tried both. Date as string and date as datetime. Did not work either. The screenshot was taken after the conversion – daniel guo Jul 12 '22 at 06:41
  • I doubt the `datum` column contains dates: In the screenshot there's a non-zero time part (the `22` after the `T`)? – Timus Jul 12 '22 at 07:29
  • I added screenshots to the question, so you can see how the column looked before and after – daniel guo Jul 12 '22 at 08:59
  • As noted in the last comment: It looks to me that you don't have dates, there's a non-zero time part? Could you add a [mre](https://stackoverflow.com/help/minimal-reproducible-example) (also look [here](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) that replicates your problem? Otherwise it's hard to diagnose what's going wrong. – Timus Jul 12 '22 at 09:17

0 Answers0