'Pandas multi-filter with pd.between_time and loc doesn't work

I made a multi-filter in panda with pd.between_time but doesn't work like other boolean condition:

df_t = pd.DataFrame(np.random.random((110,3)))
df_t['date']=pd.date_range(start='2022-01-01', end='2022-01-11', periods=110)
df_t=df_t.set_index('date')


df_t["example"]='default'

df_t.between_time('06:00:00','07:00:00',include_start=True,include_end=False)["example"]="0607"
#Try using .loc[row_indexer,col_indexer] = value instead

df_t.between_time('06:00:00','07:00:00',include_start=True,include_end=False)

#date                               0         1           2        example
#2022-01-01 06:36:19.816513761  0.044347    0.662184    0.557581    default
#2022-01-02 06:49:32.477064220  0.524793    0.485246    0.712895    default
#2022-01-09 06:09:54.495412844  0.299960    0.174559    0.806479    default
#2022-01-10 06:23:07.155963302  0.280394    0.668141    0.637077    default

if i try .loc, python raise me an error:

df_t = pd.DataFrame(np.random.random((110,3)))
df_t['date']=pd.date_range(start='2022-01-01', end='2022-01-11', periods=110)
df_t=df_t.set_index('date')
df_t["example"]='default'
df_t.loc[df_t.between_time('06:00:00','07:00:00',include_start=True,include_end=False),"example"]="0607"


Solution 1:[1]

First df_t.between_time('06:00:00','07:00:00',include_start=True,include_end=False) return a dataframe

df_t.between_time('06:00:00','07:00:00',include_start=True,include_end=False)['example'] 

That return a string serie BUT for a boolean condition, we must have a boolean serie.

I have a right result with a other way :

df_t.loc[((df_t.index.hour>=7)&(df_t.index.hour<23)),'example']="0723"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kwentinus