Expanding function based on daily groups in polars

  Kiến thức lập trình

I have an OHLC stock data in a Polars dataframe for which for each day i want to calculate the maximum expanding close between two times say ,

start = '09:15'
end = '10:15'
start_time = datetime.time.fromisoformat(start)
end_time = datetime.time.fromisoformat(end)


 # Filter and calculate the expanding max within the time range for each day

max_values = und_df2.filter((und_df2['timestamp'].dt.time() >= start_time) & (und_df2['timestamp'].dt.time() <= end_time)) 
        .group_by_dynamic('timestamp', every='1d', closed='left') 
        .agg(pl.col('Close').cum_max().alias('fbarh3')) 


# df.with_columns(pl.col(date_col).dt.truncate("1d").alias("date")) 
#              .join(
#                  df.group_by_dynamic("timestamp", every="1d").agg(agg_func.alias(alias))
#                    .with_columns(pl.col(alias).shift(shift_value))
#                    ,
#                  left_on="date", right_on="timestamp", how='left'
#              ) 
#              .drop("date")


#Join the maximum values back to the original dataframe

und_df2 = und_df2.with_columns(pl.col('timestamp').dt.truncate('1d').alias('date')).join(max_values, left_on='date', right_on='timestamp' ,  how='left' ).drop('date')

max_values is although correctly calculated which is expanding highest of close between start and end time, but since I filtered the dataframe to these specific times I lost the time component after aggregation.
How do I make it back to the original frame? (und_df2)

#== this is max_values generated 

shape: (824, 2)
timestamp   fbarh3
datetime[μs]    list[f64]
2020-03-02 00:00:00 [11356.8, 11358.45, … 11388.65]
2020-03-03 00:00:00 [11293.65, 11293.95, … 11310.7]
2020-03-04 00:00:00 [11296.95, 11296.95, … 11325.25]
2020-03-05 00:00:00 [11312.65, 11312.65, … 11312.65]
2020-03-06 00:00:00 [10894.8, 10914.85, … 10965.3]

Output should be expanding highest(cum_max) from start to end for each day. I can’t seem to join back the max_values back to und_df2.

New contributor

aakash tandon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT