| 4033 | |
| 4034 | |
| 4035 | def test_cast_timestamp_unit(): |
| 4036 | # ARROW-1680 |
| 4037 | val = datetime.now() |
| 4038 | s = pd.Series([val]) |
| 4039 | s_nyc = s.dt.tz_localize('tzlocal()').dt.tz_convert('America/New_York') |
| 4040 | |
| 4041 | us_with_tz = pa.timestamp('us', tz='America/New_York') |
| 4042 | |
| 4043 | arr = pa.Array.from_pandas(s_nyc, type=us_with_tz) |
| 4044 | |
| 4045 | # ARROW-1906 |
| 4046 | assert arr.type == us_with_tz |
| 4047 | |
| 4048 | arr2 = pa.Array.from_pandas(s, type=pa.timestamp('us')) |
| 4049 | |
| 4050 | assert arr[0].as_py() == s_nyc[0].to_pydatetime() |
| 4051 | assert arr2[0].as_py() == s[0].to_pydatetime() |
| 4052 | |
| 4053 | # Disallow truncation |
| 4054 | arr = pa.array([123123], type='int64').cast(pa.timestamp('ms')) |
| 4055 | expected = pa.array([123], type='int64').cast(pa.timestamp('s')) |
| 4056 | |
| 4057 | # sanity check that the cast worked right |
| 4058 | assert arr.type == pa.timestamp('ms') |
| 4059 | |
| 4060 | target = pa.timestamp('s') |
| 4061 | with pytest.raises(ValueError): |
| 4062 | arr.cast(target) |
| 4063 | |
| 4064 | result = arr.cast(target, safe=False) |
| 4065 | assert result.equals(expected) |
| 4066 | |
| 4067 | # ARROW-1949 |
| 4068 | series = pd.Series([pd.Timestamp(1), pd.Timestamp(10), pd.Timestamp(1000)]) |
| 4069 | expected = pa.array([0, 0, 1], type=pa.timestamp('us')) |
| 4070 | |
| 4071 | with pytest.raises(ValueError): |
| 4072 | pa.array(series, type=pa.timestamp('us')) |
| 4073 | |
| 4074 | with pytest.raises(ValueError): |
| 4075 | pa.Array.from_pandas(series, type=pa.timestamp('us')) |
| 4076 | |
| 4077 | result = pa.Array.from_pandas(series, type=pa.timestamp('us'), safe=False) |
| 4078 | assert result.equals(expected) |
| 4079 | |
| 4080 | result = pa.array(series, type=pa.timestamp('us'), safe=False) |
| 4081 | assert result.equals(expected) |
| 4082 | |
| 4083 | |
| 4084 | def test_nested_with_timestamp_tz_round_trip(): |