Python pandas._libs.tslib.Timestamp() Examples

The following are 30 code examples of pandas._libs.tslib.Timestamp(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas._libs.tslib , or try the search function .
Example #1
Source File: test_usecols.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_usecols_with_parse_dates(all_parsers, usecols):
    # see gh-9755
    data = """a,b,c,d,e
0,1,20140101,0900,4
0,1,20140102,1000,4"""
    parser = all_parsers
    parse_dates = [[1, 2]]

    cols = {
        "a": [0, 0],
        "c_d": [
            Timestamp("2014-01-01 09:00:00"),
            Timestamp("2014-01-02 10:00:00")
        ]
    }
    expected = DataFrame(cols, columns=["c_d", "a"])
    result = parser.read_csv(StringIO(data), usecols=usecols,
                             parse_dates=parse_dates)
    tm.assert_frame_equal(result, expected) 
Example #2
Source File: nanops.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _wrap_results(result, dtype):
    """ wrap our results if needed """

    if is_datetime64_dtype(dtype):
        if not isinstance(result, np.ndarray):
            result = tslib.Timestamp(result)
        else:
            result = result.view(dtype)
    elif is_timedelta64_dtype(dtype):
        if not isinstance(result, np.ndarray):

            # raise if we have a timedelta64[ns] which is too large
            if np.fabs(result) > _int64_max:
                raise ValueError("overflow in timedelta operation")

            result = tslib.Timedelta(result, unit='ns')
        else:
            result = result.astype('i8').view(dtype)

    return result 
Example #3
Source File: usecols.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_usecols_with_parse_dates_and_full_names(self):
        # See gh-9755
        s = """0,1,20140101,0900,4
        0,1,20140102,1000,4"""
        parse_dates = [[1, 2]]
        names = list('abcde')

        cols = {
            'a': [0, 0],
            'c_d': [
                Timestamp('2014-01-01 09:00:00'),
                Timestamp('2014-01-02 10:00:00')
            ]
        }
        expected = DataFrame(cols, columns=['c_d', 'a'])

        df = self.read_csv(StringIO(s), names=names,
                           usecols=[0, 2, 3],
                           parse_dates=parse_dates)
        tm.assert_frame_equal(df, expected)

        df = self.read_csv(StringIO(s), names=names,
                           usecols=[3, 0, 2],
                           parse_dates=parse_dates)
        tm.assert_frame_equal(df, expected) 
Example #4
Source File: converters.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_converters(self):
        data = """A,B,C,D
a,1,2,01/01/2009
b,3,4,01/02/2009
c,4,5,01/03/2009
"""
        result = self.read_csv(StringIO(data), converters={'D': parse_date})
        result2 = self.read_csv(StringIO(data), converters={3: parse_date})

        expected = self.read_csv(StringIO(data))
        expected['D'] = expected['D'].map(parse_date)

        assert isinstance(result['D'][0], (datetime, Timestamp))
        tm.assert_frame_equal(result, expected)
        tm.assert_frame_equal(result2, expected)

        # produce integer
        converter = lambda x: int(x.split('/')[2])
        result = self.read_csv(StringIO(data), converters={'D': converter})
        expected = self.read_csv(StringIO(data))
        expected['D'] = expected['D'].map(converter)
        tm.assert_frame_equal(result, expected) 
Example #5
Source File: test_offsets.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_last_week_of_month_on_offset():
    # GH#19036, GH#18977 _adjust_dst was incorrect for LastWeekOfMonth
    offset = LastWeekOfMonth(n=4, weekday=6)
    ts = Timestamp('1917-05-27 20:55:27.084284178+0200',
                   tz='Europe/Warsaw')
    slow = (ts + offset) - offset == ts
    fast = offset.onOffset(ts)
    assert fast == slow

    # negative n
    offset = LastWeekOfMonth(n=-4, weekday=5)
    ts = Timestamp('2005-08-27 05:01:42.799392561-0500',
                   tz='America/Rainy_River')
    slow = (ts + offset) - offset == ts
    fast = offset.onOffset(ts)
    assert fast == slow 
Example #6
Source File: test_ujson.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_datetime_units(self):
        val = datetime.datetime(2013, 8, 17, 21, 17, 12, 215504)
        stamp = Timestamp(val)

        roundtrip = ujson.decode(ujson.encode(val, date_unit='s'))
        assert roundtrip == stamp.value // 10**9

        roundtrip = ujson.decode(ujson.encode(val, date_unit='ms'))
        assert roundtrip == stamp.value // 10**6

        roundtrip = ujson.decode(ujson.encode(val, date_unit='us'))
        assert roundtrip == stamp.value // 10**3

        roundtrip = ujson.decode(ujson.encode(val, date_unit='ns'))
        assert roundtrip == stamp.value

        msg = "Invalid value 'foo' for option 'date_unit'"
        with pytest.raises(ValueError, match=msg):
            ujson.encode(val, date_unit='foo') 
Example #7
Source File: test_ujson.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_datetime_units(self):
        from pandas._libs.tslib import Timestamp

        val = datetime.datetime(2013, 8, 17, 21, 17, 12, 215504)
        stamp = Timestamp(val)

        roundtrip = ujson.decode(ujson.encode(val, date_unit='s'))
        assert roundtrip == stamp.value // 10**9

        roundtrip = ujson.decode(ujson.encode(val, date_unit='ms'))
        assert roundtrip == stamp.value // 10**6

        roundtrip = ujson.decode(ujson.encode(val, date_unit='us'))
        assert roundtrip == stamp.value // 10**3

        roundtrip = ujson.decode(ujson.encode(val, date_unit='ns'))
        assert roundtrip == stamp.value

        pytest.raises(ValueError, ujson.encode, val, date_unit='foo') 
Example #8
Source File: parse_dates.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_nat_parse(self):
        # See gh-3062
        df = DataFrame(dict({
            'A': np.asarray(lrange(10), dtype='float64'),
            'B': pd.Timestamp('20010101')}))
        df.iloc[3:6, :] = np.nan

        with tm.ensure_clean('__nat_parse_.csv') as path:
            df.to_csv(path)
            result = self.read_csv(path, index_col=0, parse_dates=['B'])
            tm.assert_frame_equal(result, df)

            expected = Series(dict(A='float64', B='datetime64[ns]'))
            tm.assert_series_equal(expected, result.dtypes)

            # test with NaT for the nan_rep
            # we don't have a method to specify the Datetime na_rep
            # (it defaults to '')
            df.to_csv(path)
            result = self.read_csv(path, index_col=0, parse_dates=['B'])
            tm.assert_frame_equal(result, df) 
Example #9
Source File: test_usecols.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_usecols_with_parse_dates_and_names(all_parsers, usecols, names):
    # see gh-9755
    s = """0,1,20140101,0900,4
0,1,20140102,1000,4"""
    parse_dates = [[1, 2]]
    parser = all_parsers

    cols = {
        "a": [0, 0],
        "c_d": [
            Timestamp("2014-01-01 09:00:00"),
            Timestamp("2014-01-02 10:00:00")
        ]
    }
    expected = DataFrame(cols, columns=["c_d", "a"])

    result = parser.read_csv(StringIO(s), names=names,
                             parse_dates=parse_dates,
                             usecols=usecols)
    tm.assert_frame_equal(result, expected) 
Example #10
Source File: test_usecols.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_usecols_with_parse_dates3(all_parsers):
    # see gh-14792
    parser = all_parsers
    data = """a,b,c,d,e,f,g,h,i,j
2016/09/21,1,1,2,3,4,5,6,7,8"""

    usecols = list("abcdefghij")
    parse_dates = [0]

    cols = {"a": Timestamp("2016-09-21"),
            "b": [1], "c": [1], "d": [2],
            "e": [3], "f": [4], "g": [5],
            "h": [6], "i": [7], "j": [8]}
    expected = DataFrame(cols, columns=usecols)

    result = parser.read_csv(StringIO(data), usecols=usecols,
                             parse_dates=parse_dates)
    tm.assert_frame_equal(result, expected) 
Example #11
Source File: test_usecols.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_usecols_with_parse_dates2(all_parsers):
    # see gh-13604
    parser = all_parsers
    data = """2008-02-07 09:40,1032.43
2008-02-07 09:50,1042.54
2008-02-07 10:00,1051.65"""

    names = ["date", "values"]
    usecols = names[:]
    parse_dates = [0]

    index = Index([Timestamp("2008-02-07 09:40"),
                   Timestamp("2008-02-07 09:50"),
                   Timestamp("2008-02-07 10:00")],
                  name="date")
    cols = {"values": [1032.43, 1042.54, 1051.65]}
    expected = DataFrame(cols, index=index)

    result = parser.read_csv(StringIO(data), parse_dates=parse_dates,
                             index_col=0, usecols=usecols,
                             header=None, names=names)
    tm.assert_frame_equal(result, expected) 
Example #12
Source File: test_multi.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def take_invalid_kwargs(self):
        vals = [['A', 'B'],
                [pd.Timestamp('2011-01-01'), pd.Timestamp('2011-01-02')]]
        idx = pd.MultiIndex.from_product(vals, names=['str', 'dt'])
        indices = [1, 2]

        msg = r"take\(\) got an unexpected keyword argument 'foo'"
        tm.assert_raises_regex(TypeError, msg, idx.take,
                               indices, foo=2)

        msg = "the 'out' parameter is not supported"
        tm.assert_raises_regex(ValueError, msg, idx.take,
                               indices, out=indices)

        msg = "the 'mode' parameter is not supported"
        tm.assert_raises_regex(ValueError, msg, idx.take,
                               indices, mode='clip') 
Example #13
Source File: test_base.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_union_sort_other_incomparable(self):
        # https://github.com/pandas-dev/pandas/issues/24959
        idx = pd.Index([1, pd.Timestamp('2000')])
        # default (sort=None)
        with tm.assert_produces_warning(RuntimeWarning):
            result = idx.union(idx[:1])

        tm.assert_index_equal(result, idx)

        # sort=None
        with tm.assert_produces_warning(RuntimeWarning):
            result = idx.union(idx[:1], sort=None)
        tm.assert_index_equal(result, idx)

        # sort=False
        result = idx.union(idx[:1], sort=False)
        tm.assert_index_equal(result, idx) 
Example #14
Source File: test_base.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_difference_incomparable(self, opname):
        a = pd.Index([3, pd.Timestamp('2000'), 1])
        b = pd.Index([2, pd.Timestamp('1999'), 1])
        op = operator.methodcaller(opname, b)

        # sort=None, the default
        result = op(a)
        expected = pd.Index([3, pd.Timestamp('2000'), 2, pd.Timestamp('1999')])
        if opname == 'difference':
            expected = expected[:2]
        tm.assert_index_equal(result, expected)

        # sort=False
        op = operator.methodcaller(opname, b, sort=False)
        result = op(a)
        tm.assert_index_equal(result, expected) 
Example #15
Source File: test_base.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_difference_incomparable(self, opname):
        a = pd.Index([3, pd.Timestamp('2000'), 1])
        b = pd.Index([2, pd.Timestamp('1999'), 1])
        op = operator.methodcaller(opname, b)

        # sort=None, the default
        result = op(a)
        expected = pd.Index([3, pd.Timestamp('2000'), 2, pd.Timestamp('1999')])
        if opname == 'difference':
            expected = expected[:2]
        tm.assert_index_equal(result, expected)

        # sort=False
        op = operator.methodcaller(opname, b, sort=False)
        result = op(a)
        tm.assert_index_equal(result, expected) 
Example #16
Source File: test_base.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_union_sort_other_incomparable(self):
        # https://github.com/pandas-dev/pandas/issues/24959
        idx = pd.Index([1, pd.Timestamp('2000')])
        # default (sort=None)
        with tm.assert_produces_warning(RuntimeWarning):
            result = idx.union(idx[:1])

        tm.assert_index_equal(result, idx)

        # sort=None
        with tm.assert_produces_warning(RuntimeWarning):
            result = idx.union(idx[:1], sort=None)
        tm.assert_index_equal(result, idx)

        # sort=False
        result = idx.union(idx[:1], sort=False)
        tm.assert_index_equal(result, idx) 
Example #17
Source File: numpy_records.py    From arctic with GNU Lesser General Public License v2.1 6 votes vote down vote up
def _to_primitive(arr, string_max_len=None, forced_dtype=None):
    if arr.dtype.hasobject:
        if len(arr) > 0 and isinstance(arr[0], Timestamp):
            return np.array([t.value for t in arr], dtype=DTN64_DTYPE)

        if forced_dtype is not None:
            casted_arr = arr.astype(dtype=forced_dtype, copy=False)
        elif string_max_len is not None:
            casted_arr = np.array(arr.astype('U{:d}'.format(string_max_len)))
        else:
            casted_arr = np.array(list(arr))

        # Pick any unwanted data conversions (e.g. np.NaN to 'nan')
        if np.array_equal(arr, casted_arr):
            return casted_arr
    return arr 
Example #18
Source File: test_offsets.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_vectorized_offset_addition(self, klass, assert_func):
        s = klass([Timestamp('2000-01-15 00:15:00', tz='US/Central'),
                   Timestamp('2000-02-15', tz='US/Central')], name='a')
        result = s + SemiMonthBegin()
        result2 = SemiMonthBegin() + s
        exp = klass([Timestamp('2000-02-01 00:15:00', tz='US/Central'),
                     Timestamp('2000-03-01', tz='US/Central')], name='a')
        assert_func(result, exp)
        assert_func(result2, exp)

        s = klass([Timestamp('2000-01-01 00:15:00', tz='US/Central'),
                   Timestamp('2000-02-01', tz='US/Central')], name='a')
        result = s + SemiMonthBegin()
        result2 = SemiMonthBegin() + s
        exp = klass([Timestamp('2000-01-15 00:15:00', tz='US/Central'),
                     Timestamp('2000-02-15', tz='US/Central')], name='a')
        assert_func(result, exp)
        assert_func(result2, exp) 
Example #19
Source File: test_offsets.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_apply_nanoseconds(self):
        tests = []

        tests.append((BusinessHour(),
                      {Timestamp('2014-07-04 15:00') + Nano(5): Timestamp(
                          '2014-07-04 16:00') + Nano(5),
                       Timestamp('2014-07-04 16:00') + Nano(5): Timestamp(
                           '2014-07-07 09:00') + Nano(5),
                       Timestamp('2014-07-04 16:00') - Nano(5): Timestamp(
                           '2014-07-04 17:00') - Nano(5)}))

        tests.append((BusinessHour(-1),
                      {Timestamp('2014-07-04 15:00') + Nano(5): Timestamp(
                          '2014-07-04 14:00') + Nano(5),
                       Timestamp('2014-07-04 10:00') + Nano(5): Timestamp(
                           '2014-07-04 09:00') + Nano(5),
                       Timestamp('2014-07-04 10:00') - Nano(5): Timestamp(
                           '2014-07-03 17:00') - Nano(5), }))

        for offset, cases in tests:
            for base, expected in compat.iteritems(cases):
                assert_offset_equal(offset, base, expected) 
Example #20
Source File: test_offsets.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_vectorized_offset_addition(self, klass, assert_func):
        s = klass([Timestamp('2000-01-15 00:15:00', tz='US/Central'),
                   Timestamp('2000-02-15', tz='US/Central')], name='a')

        result = s + SemiMonthEnd()
        result2 = SemiMonthEnd() + s
        exp = klass([Timestamp('2000-01-31 00:15:00', tz='US/Central'),
                     Timestamp('2000-02-29', tz='US/Central')], name='a')
        assert_func(result, exp)
        assert_func(result2, exp)

        s = klass([Timestamp('2000-01-01 00:15:00', tz='US/Central'),
                   Timestamp('2000-02-01', tz='US/Central')], name='a')
        result = s + SemiMonthEnd()
        result2 = SemiMonthEnd() + s
        exp = klass([Timestamp('2000-01-15 00:15:00', tz='US/Central'),
                     Timestamp('2000-02-15', tz='US/Central')], name='a')
        assert_func(result, exp)
        assert_func(result2, exp) 
Example #21
Source File: test_base.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_asof_datetime_partial(self):
        index = pd.date_range('2010-01-01', periods=2, freq='m')
        expected = Timestamp('2010-02-28')
        result = index.asof('2010-02')
        assert result == expected
        assert not isinstance(result, Index) 
Example #22
Source File: common.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _dict_compat(d):
    """
    Helper function to convert datetimelike-keyed dicts to Timestamp-keyed dict

    Parameters
    ----------
    d: dict like object

    Returns
    -------
    dict

    """
    return dict((_maybe_box_datetimelike(key), value)
                for key, value in iteritems(d)) 
Example #23
Source File: parse_dates.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_multiple_date_col_timestamp_parse(self):
        data = """05/31/2012,15:30:00.029,1306.25,1,E,0,,1306.25
05/31/2012,15:30:00.029,1306.25,8,E,0,,1306.25"""
        result = self.read_csv(StringIO(data), sep=',', header=None,
                               parse_dates=[[0, 1]], date_parser=Timestamp)

        ex_val = Timestamp('05/31/2012 15:30:00.029')
        assert result['0_1'][0] == ex_val 
Example #24
Source File: common.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_read_csv_no_index_name(self):
        df = self.read_csv(self.csv2, index_col=0, parse_dates=True)
        df2 = self.read_table(self.csv2, sep=',', index_col=0,
                              parse_dates=True)
        tm.assert_index_equal(df.columns,
                              pd.Index(['A', 'B', 'C', 'D', 'E']))
        assert isinstance(df.index[0], (datetime, np.datetime64, Timestamp))
        assert df.loc[:, ['A', 'B', 'C', 'D']].values.dtype == np.float64
        tm.assert_frame_equal(df, df2) 
Example #25
Source File: common.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_read_csv_dataframe(self):
        df = self.read_csv(self.csv1, index_col=0, parse_dates=True)
        df2 = self.read_table(self.csv1, sep=',', index_col=0,
                              parse_dates=True)
        tm.assert_index_equal(df.columns, pd.Index(['A', 'B', 'C', 'D']))
        assert df.index.name == 'index'
        assert isinstance(
            df.index[0], (datetime, np.datetime64, Timestamp))
        assert df.values.dtype == np.float64
        tm.assert_frame_equal(df, df2) 
Example #26
Source File: test_offsets.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_weekofmonth_onoffset():
    # GH#18864
    # Make sure that nanoseconds don't trip up onOffset (and with it apply)
    offset = WeekOfMonth(n=2, week=2, weekday=0)
    ts = Timestamp('1916-05-15 01:14:49.583410462+0422', tz='Asia/Qyzylorda')
    fast = offset.onOffset(ts)
    slow = (ts + offset) - offset == ts
    assert fast == slow

    # negative n
    offset = WeekOfMonth(n=-3, week=1, weekday=0)
    ts = Timestamp('1980-12-08 03:38:52.878321185+0500', tz='Asia/Oral')
    fast = offset.onOffset(ts)
    slow = (ts + offset) - offset == ts
    assert fast == slow 
Example #27
Source File: test_offsets.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_weeks_onoffset():
    # GH#18510 Week with weekday = None, normalize = False should always
    # be onOffset
    offset = Week(n=2, weekday=None)
    ts = Timestamp('1862-01-13 09:03:34.873477378+0210', tz='Africa/Lusaka')
    fast = offset.onOffset(ts)
    slow = (ts + offset) - offset == ts
    assert fast == slow

    # negative n
    offset = Week(n=2, weekday=None)
    ts = Timestamp('1856-10-24 16:18:36.556360110-0717', tz='Pacific/Easter')
    fast = offset.onOffset(ts)
    slow = (ts + offset) - offset == ts
    assert fast == slow 
Example #28
Source File: test_offsets.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_fallback_singular(self):
        # in the case of singular offsets, we don't necessarily know which utc
        # offset the new Timestamp will wind up in (the tz for 1 month may be
        # different from 1 second) so we don't specify an expected_utc_offset
        for tz, utc_offsets in self.timezone_utc_offsets.items():
            hrs_pre = utc_offsets['utc_offset_standard']
            self._test_all_offsets(n=1, tstart=self._make_timestamp(
                self.ts_pre_fallback, hrs_pre, tz), expected_utc_offset=None) 
Example #29
Source File: test_offsets.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _make_timestamp(self, string, hrs_offset, tz):
        if hrs_offset >= 0:
            offset_string = '{hrs:02d}00'.format(hrs=hrs_offset)
        else:
            offset_string = '-{hrs:02d}00'.format(hrs=-1 * hrs_offset)
        return Timestamp(string + offset_string).tz_convert(tz) 
Example #30
Source File: test_offsets.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def get_utc_offset_hours(ts):
    # take a Timestamp and compute total hours of utc offset
    o = ts.utcoffset()
    return (o.days * 24 * 3600 + o.seconds) / 3600.0