Skip to content

Commit c3789d7

Browse files
committed
mods to support dates in csv2rec and friends
svn path=/trunk/matplotlib/; revision=4755
1 parent 06e9ad2 commit c3789d7

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

lib/matplotlib/mlab.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -2129,16 +2129,26 @@ def process_skiprows(reader):
21292129

21302130
process_skiprows(reader)
21312131

2132+
dateparser = dateutil.parser.parse
21322133

21332134
def myfloat(x):
21342135
if x==missing:
21352136
return npy.nan
21362137
else:
21372138
return float(x)
21382139

2140+
def mydate(x):
2141+
# try and return a date object
2142+
d = dateparser(x)
2143+
2144+
if d.hour>0 or d.minute>0 or d.second>0:
2145+
raise ValueError('not a date')
2146+
return d.date()
2147+
2148+
21392149
def get_func(item, func):
21402150
# promote functions in this order
2141-
funcmap = {int:myfloat, myfloat:dateutil.parser.parse, dateutil.parser.parse:str}
2151+
funcmap = {int:myfloat, myfloat:mydate, mydate:dateparser, dateparser:str}
21422152
try: func(item)
21432153
except:
21442154
if func==str:
@@ -2233,17 +2243,17 @@ def tostr(self, x):
22332243
return self.toval(x)
22342244

22352245
def toval(self, x):
2236-
return repr(x)
2246+
return str(x)
22372247

22382248

2239-
class FormatString2(FormatObj):
2249+
class FormatString(FormatObj):
22402250
def tostr(self, x):
22412251
val = repr(x)
22422252
return val[1:-1]
22432253

2244-
class FormatString(FormatObj):
2245-
def tostr(self, x):
2246-
return '"%r"'%self.toval(x)
2254+
#class FormatString(FormatObj):
2255+
# def tostr(self, x):
2256+
# return '"%r"'%self.toval(x)
22472257

22482258
class FormatFormatStr(FormatObj):
22492259
def __init__(self, fmt):
@@ -2301,7 +2311,7 @@ def __init__(self, fmt='%Y-%m-%d %H:%M:%S'):
23012311
npy.float32 : FormatFloat(),
23022312
npy.float64 : FormatFloat(),
23032313
npy.object_ : FormatObj(),
2304-
npy.string_ : FormatString2(),
2314+
npy.string_ : FormatString(),
23052315
}
23062316

23072317
def get_formatd(r, formatd=None):

unit/mlab_unit.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import unittest
1+
import datetime, StringIO, unittest
22
import matplotlib.mlab as mlab
33
import numpy
4-
import StringIO
54

65
class TestMlab(unittest.TestCase):
76
def test_csv2rec_closefile(self):
@@ -21,11 +20,22 @@ def test_csv2rec_roundtrip(self):
2120
# lost precision when passing through repr(). csv2rec was
2221
# affected by this. This test will only pass on numpy >=
2322
# 1.0.5.
24-
ra=numpy.rec.array([(123, 1197346475.0137341, 'a,bc'),
25-
(456, 123.456, 'd\'ef'),
26-
(789, 0.000000001, 'ghi'),
23+
delta = datetime.timedelta(days=1)
24+
date0 = datetime.date(2007,12,16)
25+
date1 = date0 + delta
26+
date2 = date1 + delta
27+
28+
delta = datetime.timedelta(days=1)
29+
datetime0 = datetime.datetime(2007,12,16,22,29,34,924122)
30+
datetime1 = datetime0 + delta
31+
datetime2 = datetime1 + delta
32+
ra=numpy.rec.fromrecords([
33+
(123, date0, datetime0, 1197346475.0137341, 'a,bc'),
34+
(456, date1, datetime1, 123.456, 'd\'ef'),
35+
(789, date2, datetime2, 0.000000001, 'ghi'),
2736
],
28-
dtype=[('a', '<i8'), ('b', '<f8'), ('c', '|S3')])
37+
names='intdata,datedata,datetimedata,floatdata,stringdata')
38+
2939
fh = StringIO.StringIO()
3040
mlab.rec2csv( ra, fh )
3141
fh.seek(0)
@@ -36,6 +46,8 @@ def test_csv2rec_roundtrip(self):
3646
fh.seek(0)
3747
ra2 = mlab.csv2rec(fh)
3848
fh.close()
49+
#print 'ra', ra
50+
#print 'ra2', ra2
3951
for name in ra.dtype.names:
4052
if 0:
4153
print name, repr(ra[name]), repr(ra2[name])

0 commit comments

Comments
 (0)