Skip to content Skip to sidebar Skip to footer

Why My Code Didn't Select Data From Pandas Dataframe?

Why didn't my date filter work? All others filters work fine. import pandas as pd import datetime data =pd.DataFrame({ 'country': ['USA', 'USA', 'Belarus','Brazil'],

Solution 1:

datetime.date objects are not vectorised with Pandas. The docs indicate this:

Returns numpy array of python datetime.date objects

Regular Python objects are stored in object dtype series which do not support fancy date indexing. Instead, you can normalize:

data['time'] = pd.to_datetime(data['time'])
select_date = data.loc[data['time'].dt.normalize() == '2018-01-17']

You can use the same idea to iterate your dataframe by day:

for day, day_df indata.groupby(data['time'].dt.normalize()):
    # do something with day_df

Post a Comment for "Why My Code Didn't Select Data From Pandas Dataframe?"