MCPcopy Create free account
hub / github.com/apache/arrow / test_cast_timestamp_unit

Function test_cast_timestamp_unit

python/pyarrow/tests/test_pandas.py:4073–4119  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4071
4072
4073def test_cast_timestamp_unit():
4074 # ARROW-1680
4075 val = datetime.now()
4076 s = pd.Series([val])
4077 s_nyc = s.dt.tz_localize('tzlocal()').dt.tz_convert('America/New_York')
4078
4079 us_with_tz = pa.timestamp('us', tz='America/New_York')
4080
4081 arr = pa.Array.from_pandas(s_nyc, type=us_with_tz)
4082
4083 # ARROW-1906
4084 assert arr.type == us_with_tz
4085
4086 arr2 = pa.Array.from_pandas(s, type=pa.timestamp('us'))
4087
4088 assert arr[0].as_py() == s_nyc[0].to_pydatetime()
4089 assert arr2[0].as_py() == s[0].to_pydatetime()
4090
4091 # Disallow truncation
4092 arr = pa.array([123123], type='int64').cast(pa.timestamp('ms'))
4093 expected = pa.array([123], type='int64').cast(pa.timestamp('s'))
4094
4095 # sanity check that the cast worked right
4096 assert arr.type == pa.timestamp('ms')
4097
4098 target = pa.timestamp('s')
4099 with pytest.raises(ValueError):
4100 arr.cast(target)
4101
4102 result = arr.cast(target, safe=False)
4103 assert result.equals(expected)
4104
4105 # ARROW-1949
4106 series = pd.Series([pd.Timestamp(1), pd.Timestamp(10), pd.Timestamp(1000)])
4107 expected = pa.array([0, 0, 1], type=pa.timestamp('us'))
4108
4109 with pytest.raises(ValueError):
4110 pa.array(series, type=pa.timestamp('us'))
4111
4112 with pytest.raises(ValueError):
4113 pa.Array.from_pandas(series, type=pa.timestamp('us'))
4114
4115 result = pa.Array.from_pandas(series, type=pa.timestamp('us'), safe=False)
4116 assert result.equals(expected)
4117
4118 result = pa.array(series, type=pa.timestamp('us'), safe=False)
4119 assert result.equals(expected)
4120
4121
4122def test_nested_with_timestamp_tz_round_trip():

Callers

nothing calls this directly

Calls 5

as_pyMethod · 0.80
equalsMethod · 0.80
castMethod · 0.45
arrayMethod · 0.45
TimestampMethod · 0.45

Tested by

no test coverage detected