Beispiel #1
0
 def test_explicit_dtype(self):
     "Test tsfromtxt with an explicit dtype."
     dstr = "2009-01-14 12:00; 23; 46"
     test = tsfromtxt(StringIO.StringIO(dstr,),
                      delimiter=";", datecols=0, freq='H')
     self.failUnless(test.dtype.names is not None)
     assert_equal(test['f1'], 23)
     assert_equal(test['f2'], 46)
     #
     dstr = "2009-01-14 12:00; 23; 46"
     test = tsfromtxt(StringIO.StringIO(dstr,),
                      delimiter=";", datecols=0, dtype=int, freq='H')
     assert(test.dtype.names is None)
     assert_equal(test, [[23, 46]])
 def test_missing_values_no_names(self):
     "Make sure that floating point missing values are kept if no names"
     a = "AAA,2010,1,-9\nBBB,2010,2,2"
     dateconv = lambda y, m: Date('M', year=int(y), month=int(m))
     kwargs = dict(freq='M', delimiter=',', dateconverter=dateconv,
                   missing_values= -9,
                   datecols=(1, 2), usecols=(1, 2, 3), names="A")
     test = tsfromtxt(StringIO.StringIO(a), **kwargs)
     assert_equal(test.mask, np.array([(1,), (0,)], dtype=[('A', bool)]))
 def test_explicit_names_with_usecols(self):
     "Make sure the proper names are given to entries when usecols is not None"
     a = "AAA,2010,1,1,2,3\nBBB,2010,2,10,20,30"
     dateconv = lambda y, m: Date('M', year=int(y), month=int(m))
     kwargs = dict(freq='M', delimiter=',', dateconverter=dateconv,
                   datecols=(1, 2), usecols=(1, 2, 3, 5), names="A, C")
     test = tsfromtxt(StringIO.StringIO(a), **kwargs)
     ctrl = time_series([(1, 3), (10, 30)],
                        start_date=Date('M', '2010-01'),
                        dtype=[('A', int), ('C', int)])
     assert_equal(test, ctrl)
Beispiel #4
0
 def test_explicit_names(self):
     "Test w/ explicit dtype (and explicit float)"
     data = "200510, 380.00, 386.30\n200511, 386.85, 388.55\n"
     dconverter = lambda x: Date("M", "%s-%s" % (x[:4], x[4:6]))
     kwargs = dict(delimiter=",", datecols=0, dateconverter=dconverter)
     ctrl = time_series([(380., 386.30), (386.85, 388.55)],
                        start_date="2005-10", freq="M",
                        dtype=[('open', "f4"), ('close', "f4")])
     test = tsfromtxt(StringIO.StringIO(data),
                      dtype=[('open', "f4"), ('close', "f4")],
                      **kwargs)
     assert_equal(test, ctrl)
Beispiel #5
0
 def test_with_converter(self):
     "Test tsfromtxt w/ an explicit converter"
     input = StringIO.StringIO("2001-01, 00mm\n2002-01, 10mm\n2003-01, 00mm")
     conv = converter = {1: lambda s:float(s.split('mm')[0])}
     test = tsfromtxt(input, delimiter=',', converters=conv, datecols=0,
                      freq='M', dtype=float)
     control = time_series([0., 10., 0.],
                           dates=['2001-01', '2002-01', '2003-01'],
                           freq='M')
     assert(isinstance(test, TimeSeries))
     assert_equal(test, control)
     assert_equal(test.dates, control.dates)
Beispiel #6
0
 def test_with_negative_datecols(self):
     "Test negative datecols"
     data = "380.00, 386.30, 200510\n386.85, 388.55, 200511\n"
     dconverter = lambda x: Date("M", "%s-%s" % (x[:4], x[4:6]))
     kwargs = dict(delimiter=",", datecols= -1, dateconverter=dconverter)
     ctrl = time_series([(380., 386.30), (386.85, 388.55)],
                        start_date="2005-10", freq="M",
                        dtype=[('open', "f4"), ('close', "f4")])
     test = tsfromtxt(StringIO.StringIO(data),
                      dtype=[('open', "f4"), ('close', "f4")],
                      **kwargs)
     assert_equal(test, ctrl)
Beispiel #7
0
 def test_explicit_dtype_with_explicit_float(self):
     "Test w/ explicit dtype (and explicit float)"
     data = "200510, 380.00, 386.30\n200511, 386.85, 388.55\n"
     dconverter = lambda x: Date("M", "%s-%s" % (x[:4], x[4:6]))
     kwargs = dict(delimiter=",", datecols=0, dateconverter=dconverter)
     ctrl = time_series([(380., 386.30), (386.85, 388.55)],
                        start_date="2005-10",
                        freq="M",
                        dtype=[('open', "f4"), ('close', "f4")])
     test = tsfromtxt(StringIO.StringIO(data),
                      dtype=[('open', "f4"), ('close', "f4")],
                      **kwargs)
     assert_equal(test, ctrl)
Beispiel #8
0
 def test_with_negative_datecols(self):
     "Test negative datecols"
     data = "380.00, 386.30, 200510\n386.85, 388.55, 200511\n"
     dconverter = lambda x: Date("M", "%s-%s" % (x[:4], x[4:6]))
     kwargs = dict(delimiter=",", datecols=-1, dateconverter=dconverter)
     ctrl = time_series([(380., 386.30), (386.85, 388.55)],
                        start_date="2005-10",
                        freq="M",
                        dtype=[('open', "f4"), ('close', "f4")])
     test = tsfromtxt(StringIO.StringIO(data),
                      dtype=[('open', "f4"), ('close', "f4")],
                      **kwargs)
     assert_equal(test, ctrl)
Beispiel #9
0
    def test_nodateinfo(self):
        # No dates column specified: crash.
        "Test no date info"
        fcontent = StringIO.StringIO("""#
'Dates', 'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
'2007-01', 'strings',1,1.0,'mixed column',,1
'2007-02', 'with embedded "double quotes"',2,2.0,1.0,,1
'2007-03', 'strings',3,3.0E5,3,,1
'2007-05','strings',4,-1e-10,,,1
""")
        try:
            test = tsfromtxt(fcontent, delimiter=",", names="A,B,C,D,E,F,G")
        except TypeError:
            pass
Beispiel #10
0
    def test_nodateinfo(self):
        # No dates column specified: crash.
        "Test no date info"
        fcontent = StringIO.StringIO("""#
'Dates', 'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
'2007-01', 'strings',1,1.0,'mixed column',,1
'2007-02', 'with embedded "double quotes"',2,2.0,1.0,,1
'2007-03', 'strings',3,3.0E5,3,,1
'2007-05','strings',4,-1e-10,,,1
""")
        try:
            test = tsfromtxt(fcontent, delimiter=",", names="A,B,C,D,E,F,G")
        except TypeError:
            pass
Beispiel #11
0
 def test_explicit_structured_dtype(self):
     "Test tsfromtxt with an explicit structured dtype"
     data = StringIO.StringIO("2007,12,31,24,34,56,0")
     dateconverter = lambda y, m, d: Date('D', year=int(y), month=int(m),
                                          day=int(d))
     ndtype = [('tobs', int), ('tmin', float), ('tmax', float),
               ('rain', float)]
     test = tsfromtxt(data, delimiter=",", dtype=ndtype,
                      datecols=(0, 1, 2), dateconverter=dateconverter)
     control = time_series([(24, 34.0, 56.0, 0.0)],
                           dtype=ndtype,
                           start_date=Date('D', '2007-12-01'))
     assert_equal(test, control)
     #
     data = StringIO.StringIO("2007,12,31,24,34,56,0")
     ndtype = [('tobs', int), ('tmin', int), ('tmax', int),
               ('rain', complex)]
     test = tsfromtxt(data, delimiter=",", dtype=ndtype,
                      datecols=(0, 1, 2), dateconverter=dateconverter)
     control = time_series([(24, 34.0, 56.0, 0.0)],
                           dtype=ndtype,
                           start_date=Date('D', '2007-12-01'))
     assert_equal(test, control)
Beispiel #12
0
 def test_dates_on_several_columns(self):
     "Test tsfromtxt when the date spans several columns."
     datatxt = """
     2001, 01, 0.0, 10.
     2001, 02, 1.1, 11.
     2001, 02, 2.2, 12.
     """
     data = StringIO.StringIO(datatxt)
     dateconverter = lambda y, m: Date('M', year=int(y), month=int(m))
     test = tsfromtxt(data, delimiter=',', dtype=float, datecols=(0, 1),
                      dateconverter=dateconverter)
     assert_equal(test, [[0., 10.], [1.1, 11.], [2.2, 12.]])
     assert_equal(test.dates,
                  date_array(['2001-01', '2001-02', '2001-02'], freq='M'))
Beispiel #13
0
 def test_explicit_names_with_usecols(self):
     "Make sure the proper names are given to entries when usecols is not None"
     a = "AAA,2010,1,1,2,3\nBBB,2010,2,10,20,30"
     dateconv = lambda y, m: Date('M', year=int(y), month=int(m))
     kwargs = dict(freq='M',
                   delimiter=',',
                   dateconverter=dateconv,
                   datecols=(1, 2),
                   usecols=(1, 2, 3, 5),
                   names="A, C")
     test = tsfromtxt(StringIO.StringIO(a), **kwargs)
     ctrl = time_series([(1, 3), (10, 30)],
                        start_date=Date('M', '2010-01'),
                        dtype=[('A', int), ('C', int)])
     assert_equal(test, ctrl)
Beispiel #14
0
    def test_with_datecols(self):
        "Test two datecols"
        fcontent = StringIO.StringIO("""
year, month, A, B
2009, 01, 1, 1.
2009, 03, 3, 3.
""")
        dateconv = lambda y, m: Date("M", year=int(y), month=int(m))
        test = tsfromtxt(fcontent, delimiter=",", skip_header=1, names=True,
                         converters={'dates': dateconv}, datecols=(0, 1))
        dates = date_array(['2009-01', '2009-03'], freq='M')
        assert_equal(test.dates.tovalue(), dates)
        assert_equal(test['A'], [1, 3])
        assert_equal(test['B'], [1., 3.])
        assert_equal(test.dtype, np.dtype([('A', int), ('B', float)]))
Beispiel #15
0
 def test_with_converter(self):
     "Test tsfromtxt w/ an explicit converter"
     input = StringIO.StringIO(
         "2001-01, 00mm\n2002-01, 10mm\n2003-01, 00mm")
     conv = converter = {1: lambda s: float(s.split('mm')[0])}
     test = tsfromtxt(input,
                      delimiter=',',
                      converters=conv,
                      datecols=0,
                      freq='M',
                      dtype=float)
     control = time_series([0., 10., 0.],
                           dates=['2001-01', '2002-01', '2003-01'],
                           freq='M')
     assert (isinstance(test, TimeSeries))
     assert_equal(test, control)
     assert_equal(test.dates, control.dates)
Beispiel #16
0
 def test_dates_on_several_columns(self):
     "Test tsfromtxt when the date spans several columns."
     datatxt = """
     2001, 01, 0.0, 10.
     2001, 02, 1.1, 11.
     2001, 02, 2.2, 12.
     """
     data = StringIO.StringIO(datatxt)
     dateconverter = lambda y, m: Date('M', year=int(y), month=int(m))
     test = tsfromtxt(data,
                      delimiter=',',
                      dtype=float,
                      datecols=(0, 1),
                      dateconverter=dateconverter)
     assert_equal(test, [[0., 10.], [1.1, 11.], [2.2, 12.]])
     assert_equal(test.dates,
                  date_array(['2001-01', '2001-02', '2001-02'], freq='M'))
Beispiel #17
0
    def test_with_datecols(self):
        "Test two datecols"
        fcontent = StringIO.StringIO("""
year, month, A, B
2009, 01, 1, 1.
2009, 03, 3, 3.
""")
        dateconv = lambda y, m: Date("M", year=int(y), month=int(m))
        test = tsfromtxt(fcontent,
                         delimiter=",",
                         skip_header=1,
                         names=True,
                         converters={'dates': dateconv},
                         datecols=(0, 1))
        dates = date_array(['2009-01', '2009-03'], freq='M')
        assert_equal(test.dates.tovalue(), dates)
        assert_equal(test['A'], [1, 3])
        assert_equal(test['B'], [1., 3.])
        assert_equal(test.dtype, np.dtype([('A', int), ('B', float)]))
Beispiel #18
0
    def test_with_names(self):
        "Tests w/ names"
        fcontent = StringIO.StringIO("""#
'Dates', 'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
'2007-01', 'strings',1,1.0,'mixed column',,1
'2007-02', 'with embedded "double quotes"',2,2.0,1.0,,1
'2007-03', 'strings',3,3.0E5,3,,1
'2007-05','strings',4,-1e-10,,,1
""")
        test = tsfromtxt(fcontent, delimiter=",", datecols=0, skip_header=2,
                         names="A,B,C,D,E,F", freq='M')
        assert(isinstance(test, TimeSeries))
        dlist = ['2007-%02i' % i for i in (1, 2, 3, 5)]
        assert_equal(test.dates.tovalue(),
                     date_array(dlist, freq='M').tovalue())
        assert_equal(test.dtype.names, ['A', 'B', 'C', 'D', 'E', 'F'])
        assert_equal(test['F'], [1, 1, 1, 1])
        assert_equal(test['E'].mask, [1, 1, 1, 1])
        assert_equal(test['C'], [1, 2, 300000, -1e-10])
Beispiel #19
0
    def test_without_names(self):
        "Test w/o names"
        fcontent = StringIO.StringIO("""#
'Dates', 'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
'2007-01', 'strings',1,1.0,'mixed column',,1
'2007-02', 'with embedded "double quotes"',2,2.0,1.0,,1
'2007-03', 'strings',3,3.0E5,3,,1
'2007-05','strings',4,-1e-10,,,1
""")
        test = tsfromtxt(fcontent,
                         delimiter=",",
                         skip_header=1,
                         names=True,
                         freq='M')
        assert (isinstance(test, TimeSeries))
        dlist = ['2007-%02i' % i for i in (1, 2, 3, 5)]
        assert_equal(test.dates.tovalue(),
                     date_array(dlist, freq='M').tovalue())
        assert_equal(test.dtype.names,
                     ['One_S', 'Two_I', 'Three_F', 'Four_M', 'Five_', 'Six_C'])
        assert_equal(test['Six_C'], [1, 1, 1, 1])
        assert_equal(test['Five_'].mask, [1, 1, 1, 1])
        assert_equal(test['Three_F'], [1, 2, 300000, -1e-10])
Beispiel #20
0
 def test_unsorted_input(self):
     "Test tsfromtxt when the dates of the input are not sorted."
     datatxt = """dates,a,b
                 2007-04-02 01:00,,0.
                 2007-04-02 02:00,2.,20
                 2007-04-02 03:00,,
                 2007-04-02 00:00,0.,10.
                 2007-04-02 03:00,3.,30
                 2007-04-02 01:00,1.,10
                 2007-04-02 02:00,,
                 """
     data = StringIO.StringIO(datatxt)
     dates = [Date('H', '2007-04-02 0%i:00' % hour)
              for hour in (1, 2, 3, 0, 1, 2)]
     controla = ma.masked_values([ 0, -1, 1, 2, -1, -1, 3], -1)
     controlb = ma.masked_values([10, 0, 10, 20, -1, -1, 30], -1)
     #
     data = StringIO.StringIO(datatxt)
     test = tsfromtxt(data, delimiter=',', names=True, freq='H')
     assert_equal(test.dtype.names, ['a', 'b'])
     assert_equal(test['a'], controla)
     assert_equal(test['a'].mask, controla.mask)
     assert_equal(test['b'], controlb)
     assert_equal(test['b'].mask, controlb.mask)