本文整理汇总了Python中pandas._libs.tslib.Timestamp方法的典型用法代码示例。如果您正苦于以下问题:Python tslib.Timestamp方法的具体用法?Python tslib.Timestamp怎么用?Python tslib.Timestamp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas._libs.tslib
的用法示例。
在下文中一共展示了tslib.Timestamp方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _to_primitive
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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
示例2: test_union_sort_other_incomparable
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例3: test_difference_incomparable
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例4: test_usecols_with_parse_dates
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例5: test_usecols_with_parse_dates2
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例6: test_usecols_with_parse_dates3
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例7: test_usecols_with_parse_dates_and_names
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例8: test_datetime_units
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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')
示例9: take_invalid_kwargs
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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')
示例10: test_apply_nanoseconds
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例11: test_vectorized_offset_addition
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例12: test_last_week_of_month_on_offset
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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
示例13: test_converters
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例14: test_usecols_with_parse_dates_and_full_names
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)
示例15: test_nat_parse
# 需要导入模块: from pandas._libs import tslib [as 别名]
# 或者: from pandas._libs.tslib import Timestamp [as 别名]
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)