Python Forum
Return the [Date] that within the last 14 days
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return the [Date] that within the last 14 days
#1
Hi, I have a column [Date] in df that return date in this format : 20220628T065216.437 GMT
I need to return the [Date] that within the last 3 days,
tried with existing format : 20220628T065216.437 GMT
date = [Date] >= datetime.now() - timedelta(hours=72, minutes=0)]
but received errors , can I still use it somehow or do I need to convert it , is so can you please help with how ? Thx!
Reply
#2
Please show what you have tried.
Reply
#3
(Dec-07-2022, 01:36 PM)deanhystad Wrote: Please show what you have tried.


date = [Date] >= datetime.now() - timedelta(hours=72, minutes=0)]
Reply
#4
Can do it like this and Pandas has it own Date/Time stuff build in.
import pandas as pd
from io import StringIO

data = StringIO('''\
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=' ')
# Make sure that it is Pandas <datetime64[ns]> format
df['Date']= pd.to_datetime(df['Date'])
# From today
>>> 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
2 2022-12-05  0.000131  1.2.44  1.2  150.0     NaN

# Use fixed date
>>> df[df["Date"] > (pd.to_datetime('2022-12-7') - 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
2 2022-12-05  0.000131  1.2.44  1.2  150.0     NaN
Reply
#5
(Dec-07-2022, 10:27 PM)snippsat Wrote: Can do it like this and Pandas has it own Date/Time stuff build in.
import pandas as pd
from io import StringIO

data = StringIO('''\
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=' ')
# Make sure that it is Pandas <datetime64[ns]> format
df['Date']= pd.to_datetime(df['Date'])
# From today
>>> 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
2 2022-12-05  0.000131  1.2.44  1.2  150.0     NaN

# Use fixed date
>>> df[df["Date"] > (pd.to_datetime('2022-12-7') - 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
2 2022-12-05  0.000131  1.2.44  1.2  150.0     NaN

Thx!
however, not sure I understand your solution ,let me clarify,
I have this column value df[Date] ,and I tried to use it in here :

Date = df[Date]
df = Date > (pd.to_datetime('today') - pd.Timedelta(days=3))

and I am greeting error :
TypeError: '>' not supported between instances of 'str' and 'Timestamp'


can you please tell me what to replace with what ?
Thx!
Reply
#6
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 231 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Python date format changes to date & time 1418 4 605 Jan-20-2024, 04:45 AM
Last Post: 1418
  How split N days between specified start & end days SriRajesh 2 1,336 May-06-2022, 02:12 PM
Last Post: SriRajesh
  Date format and past date check function Turtle 5 4,257 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to add previous date infront of every unique customer id's invoice date ur_enegmatic 1 2,236 Feb-06-2021, 10:48 PM
Last Post: eddywinch82
Brick How To Sum Numbers Of Next 7 Days Developer_2018 7 2,741 Oct-19-2020, 02:54 PM
Last Post: Developer_2018
  How to print n days back date at give time Mekala 1 1,987 Oct-10-2020, 03:35 AM
Last Post: bowlofred
  How to add date and years(integer) to get a date NG0824 4 2,866 Sep-03-2020, 02:25 PM
Last Post: NG0824
  Substracting today's date from a date in column of dates to get an integer value firebird 1 2,131 Jul-04-2019, 06:54 PM
Last Post: Axel_Erfurt
  How to change existing date to current date in a filename? shankar455 1 2,296 Apr-17-2019, 01:53 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020