(Dec-08-2022, 10:31 AM)bnadir55 Wrote: I have this column value df[Date] ,and I tried to use it in here :
Date = df[Date]
You can not do this,and that's not what i am doing.
Like this:
df['Date'] = pd.to_datetime(df['Date'])
From start and always check
dtypes
.
import pandas as pd
from io import StringIO
data = StringIO('''\
[python]Date Tval Rnum Min Blend Result
12/7/2022 0.000091 2.5.2 9 0.1 25.0
12/6/2022 0.000131 1.9.7 5 0.5 50.0
12/5/2022 0.000131 1.2.44 1.2 150.0
12/4/2022 0.000555 1.3.44 1.4 17.0''')
df = pd.read_csv(data, sep=' ')
Use.
>>> df
Date Tval Rnum Min Blend Result
0 12/7/2022 0.000091 2.5.2 9.0 0.1 25.0
1 12/6/2022 0.000131 1.9.7 5.0 0.5 50.0
2 12/5/2022 0.000131 1.2.44 1.2 150.0 NaN
3 12/4/2022 0.000555 1.3.44 1.4 17.0 NaN
>>> df.dtypes
Date object
Tval float64
Rnum object
Min float64
Blend float64
Result float64
dtype: object
See that Date is
object
,this is not correct should be
datetime
.
>>> df['Date'] = pd.to_datetime(df['Date'])
>>> df.dtypes
Date datetime64[ns]
Tval float64
Rnum object
Min float64
Blend float64
Result float64
dtype: object
No is Date column ok,and then it will work.
As one day has past get 2.
>>> df[df["Date"] > (pd.to_datetime('today') - pd.Timedelta(days=3))]
Date Tval Rnum Min Blend Result
0 2022-12-07 0.000091 2.5.2 9.0 0.1 25.0
1 2022-12-06 0.000131 1.9.7 5.0 0.5 50.0
>>> df[df["Date"] > (pd.to_datetime('today') - pd.Timedelta(days=4))]
Date Tval Rnum Min Blend Result
0 2022-12-07 0.000091 2.5.2 9.0 0.1 25.0
1 2022-12-06 0.000131 1.9.7 5.0 0.5 50.0
2 2022-12-05 0.000131 1.2.44 1.2 150.0 NaN