dak
dak

Reputation: 199

Select rows in a dataframe by date

For a given dataframe with a datetime column (2018-09-09 06:15:00), I would like to find all the rows that match a given date "2018-01-08".

I have tried something like this:

def get_connections(df, mydate):
    connections = df.loc[(df['dates']) == mydate]
    return connections

Upvotes: 1

Views: 63

Answers (2)

Kuldip Chaudhari
Kuldip Chaudhari

Reputation: 1112

try this

from datetime as datetime as dt
df['dates'] = df['dates'].apply(dt.fromisoformat)

def get_connections(df, mydate):
    connections = df.loc[df['dates'].apply(dt.date) == dt.strptime(mydate, '%Y-%m-%d')]
    return connections
get_connections(datestring)

Upvotes: 1

sushanth
sushanth

Reputation: 8302

Here is a solution,

import pandas as pd

df = pd.DataFrame({"dates":["2018-09-09 06:15:00", "2018-01-08 06:15:00"]})

df[pd.to_datetime(df.dates).dt.strftime("%Y-%m-%d").eq("2018-01-08")]

                 dates
1  2018-01-08 06:15:00

Upvotes: 2

Related Questions