Python Forum
Problem with datetime module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with datetime module
#1
Hello,

i have a csv file. It represents events that happen in France. One column contains date written like so 'YYYY-MM-DD'. I want to delete every row that has a date difference of at least 60 days from today.

I have created this program that from the string 'YYYY-MM-DD' return the difference in days from today.

date ='2018-08-26'
liste = date.split('-')
print(liste)
liste2 = []
liste2.append(int(liste[0]))
liste2.append(int(liste[1].replace('0','')))
liste2.append(int(liste[2].replace('0','')))
print(liste2)
test = datetime.now() -  datetime(liste2[0], liste2[1], liste2[2])
print(test)
if test[0] > 60 :
    print("delete")
And I get this error:

['2018', '08', '26']
[2018, 8, 26]
-33 days, 9:52:04.046700
Traceback (most recent call last):
  File "untitled2.py", line 20, in <module>
    if test[0] > 60 :
TypeError: 'datetime.timedelta' object is not subscriptable
I don't know how to "extract" the number of days, do you have any idea?

Thank you
Reply
#2
import datetime

def is_more_60days(my_date):
    test_date = datetime.datetime.strptime(my_date, '%Y-%m-%d')
    date_diff = datetime.datetime.now() - test_date
    if date_diff > datetime.timedelta(days=60):
        print("delete {}".format(my_date))
    return date_diff.days > 60

test_date ='2017-08-26'
test_date2 = '2018-05-30'

print('{} is more than 60 days away: {}'.format(test_date, is_more_60days(test_date)))
print('{} is more than 60 days away: {}'.format(test_date2, is_more_60days(test_date2)))
compare timedelta.days with 60 or compare two timedeltas (I use both ways in the example above)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Oh ok I see :)

Thank you !
Reply
#4
import datetime
   ...
    date_diff = datetime.datetime.now() - test_date
   ...
 
I was writing similar answer but was ninjad by buran :-). Very good answer and I like it.

Observation / nitpicking: it is matter of preference but one option is to use datetime.datetime.today() which is for me personally more in line with 'explicit is better than implicit'. I am not speaking english natively so for me 'today' is associated with date and 'now' with time. However, it is a matter of expected behaviour of code rather than personal biases.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with memory_graph module akbarza 3 298 Mar-04-2024, 10:15 AM
Last Post: snippsat
  datetime module question jacksfrustration 10 1,608 Jan-12-2024, 04:54 AM
Last Post: deanhystad
  problem using coloeama module akbarza 3 514 Jan-08-2024, 07:31 AM
Last Post: akbarza
  problem in using subprocess module akbarza 5 944 Sep-24-2023, 02:02 PM
Last Post: snippsat
  problem in import module from other folder akbarza 5 1,260 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  problem in using pyqrcode module to create QRcode akbarza 9 1,798 Aug-23-2023, 04:17 PM
Last Post: snippsat
  Problem with Pyinstaller. No module named '_tkinter' tonynapoli2309 0 934 May-15-2023, 02:38 PM
Last Post: tonynapoli2309
  Problem with module time and leap seconds Pedroski55 3 1,189 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
  Problem with datetime [SOLVED] AlphaInc 3 2,543 Apr-22-2022, 01:33 PM
Last Post: snippsat
  2-dataframe, datetime lookup problem Mark17 0 1,215 Jan-27-2022, 01:02 AM
Last Post: Mark17

Forum Jump:

User Panel Messages

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