0

My python code shown below is not retrieving any dates before the year 1900. I learned that this is a limitation of datetime.strptime().

I have tried to follow some of the workaround mentioned in similar posts , Is there any way to use a strftime-like function for dates before 1900 in Python? but they seem a bit complicated for me to follow. I also learned that there is a patch available to fix this issue. https://bugs.python.org/file10253/strftime-pre-1900.patch

I tried to install the patch in Pycharm by copying the patch to a text file, but I am getting the following error message. Any ideas on what I need to do to run the patch successfully to get dates before 1900 ?

Pycharm Patch Error Screenshot

My Code:

from datetime import datetime

import csv

with open('train.csv', 'r') as f_input, open('sample.txt', 'w') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)

for row in csv_input:
    for date_format in ['%Y']:
        try:
            converted = datetime.strptime(row[3], date_format)
            csv_output.writerow([row[0], row[1], row[2], converted.strftime(date_format)])
        except ValueError:
            pass
Niall Cosgrove
  • 1,273
  • 1
  • 15
  • 24
user8929822
  • 273
  • 1
  • 3
  • 13
  • Error messages and other results should be part of the text of the question. External images are not great for these sorts of questions. –  Dec 04 '17 at 20:03

1 Answers1

1

I realize this isn't what you asked, but I'll put it out there anyway since I'm assuming that if the answers to the linked question are too complicated to follow, the idea to patch your problem away successfully is probably unrealistic.

The limitation you are seeing is present in Python2:

Python 2.7.12 (default, Nov 20 2017, 18:23:56) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900

It is somewhat rectified in Python3.2, and fully rectified in Python3.3 and onward:

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
'1899-01-01'

Parsing works too:

>>> d = datetime.strptime('0113-01-01','%Y-%m-%d')
>>> d
datetime.datetime(113, 1, 1, 0, 0)
>>> d.isoformat()
'0113-01-01T00:00:00'
>>> d.strftime('%Y-%m-%d')
'113-01-01'

So if that is an acceptable option, you could switch over to Python3.3+ and you won't have this issue.

bgse
  • 8,237
  • 2
  • 37
  • 39
  • Thanks for reaching out. I am using the python 3.6 project interpreter in PyCharm. I have the following packages installed for the 3.6 interpreter - numpy, pandas, pip, Regex-Dict, python-dateutil, pytz, setuptools, six. – user8929822 Dec 05 '17 at 03:41
  • @user8929822 Are you sure you are using the 3.6 interpreter when running your project? If you are, you should not see the issue you mention in your question. – bgse Dec 06 '17 at 09:28
  • Using the 3.6 interpreter worked for me. I was using 2.7 earlier. Thanks. – user8929822 Jan 30 '18 at 19:39