Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc38288

Browse files
committed
Creation of fix to #6184 - csv2rec bug
yearfirst & dayfirst date interpretation bug for datetime strings - csv2rec does not take yearfirst or dayfirst into account when interpreting datetime strings with a time component, so for ambiguous cases, the month and day get transposed. Test added also.
1 parent 36a6d81 commit bc38288

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

lib/matplotlib/mlab.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2838,7 +2838,8 @@ def mydateparser(x):
28382838
# try and return a datetime object
28392839
d = dateparser(x, dayfirst=dayfirst, yearfirst=yearfirst)
28402840
return d
2841-
mydateparser = with_default_value(mydateparser, datetime.datetime(1,1,1,0,0,0))
2841+
2842+
mydateparser = with_default_value(mydateparser, datetime.datetime(1, 1, 1))
28422843

28432844
myfloat = with_default_value(float, np.nan)
28442845
myint = with_default_value(int, -1)

lib/matplotlib/tests/test_mlab.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,50 @@ def test_csv2rec_names_with_comments(self):
346346
assert len(array) == 2
347347
assert len(array.dtype) == 3
348348

349+
def test_csv2rec_usdate(self):
350+
self.fd.write('01/11/14\n' +
351+
'03/05/76 12:00:01 AM\n' +
352+
'07/09/83 5:17:34 PM\n' +
353+
'06/20/2054 2:31:45 PM\n' +
354+
'10/31/00 11:50:23 AM\n')
355+
expected = [datetime.datetime(2014, 1, 11, 0, 0),
356+
datetime.datetime(1976, 3, 5, 0, 0, 1),
357+
datetime.datetime(1983, 7, 9, 17, 17, 34),
358+
datetime.datetime(2054, 6, 20, 14, 31, 45),
359+
datetime.datetime(2000, 10, 31, 11, 50, 23)]
360+
self.fd.seek(0)
361+
array = mlab.csv2rec(self.fd, names='a')
362+
assert_array_equal(array['a'].tolist(), expected)
363+
364+
def test_csv2rec_dayfirst(self):
365+
self.fd.write('11/01/14\n' +
366+
'05/03/76 12:00:01 AM\n' +
367+
'09/07/83 5:17:34 PM\n' +
368+
'20/06/2054 2:31:45 PM\n' +
369+
'31/10/00 11:50:23 AM\n')
370+
expected = [datetime.datetime(2014, 1, 11, 0, 0),
371+
datetime.datetime(1976, 3, 5, 0, 0, 1),
372+
datetime.datetime(1983, 7, 9, 17, 17, 34),
373+
datetime.datetime(2054, 6, 20, 14, 31, 45),
374+
datetime.datetime(2000, 10, 31, 11, 50, 23)]
375+
self.fd.seek(0)
376+
array = mlab.csv2rec(self.fd, names='a', dayfirst = True)
377+
assert_array_equal(array['a'].tolist(), expected)
378+
379+
def test_csv2rec_yearfirst(self):
380+
self.fd.write('14/01/11\n' +
381+
'76/03/05 12:00:01 AM\n' +
382+
'83/07/09 5:17:34 PM\n' +
383+
'2054/06/20 2:31:45 PM\n' +
384+
'00/10/31 11:50:23 AM\n')
385+
expected = [datetime.datetime(2014, 1, 11, 0, 0),
386+
datetime.datetime(1976, 3, 5, 0, 0, 1),
387+
datetime.datetime(1983, 7, 9, 17, 17, 34),
388+
datetime.datetime(2054, 6, 20, 14, 31, 45),
389+
datetime.datetime(2000, 10, 31, 11, 50, 23)]
390+
self.fd.seek(0)
391+
array = mlab.csv2rec(self.fd, names='a', yearfirst = True)
392+
assert_array_equal(array['a'].tolist(), expected)
349393

350394
class window_testcase(CleanupTestCase):
351395
def setUp(self):

0 commit comments

Comments
 (0)