|
45 | 45 | except NameError: # py3
|
46 | 46 | basestring = str
|
47 | 47 |
|
48 |
| -def ints_to_pydatetime(ndarray[int64_t] arr): |
| 48 | +def ints_to_pydatetime(ndarray[int64_t] arr, tz=None): |
49 | 49 | cdef:
|
50 | 50 | Py_ssize_t i, n = len(arr)
|
51 | 51 | pandas_datetimestruct dts
|
52 | 52 | ndarray[object] result = np.empty(n, dtype=object)
|
53 | 53 |
|
54 |
| - for i in range(n): |
55 |
| - pandas_datetime_to_datetimestruct(arr[i], PANDAS_FR_ns, &dts) |
56 |
| - result[i] = datetime(dts.year, dts.month, dts.day, |
57 |
| - dts.hour, dts.min, dts.sec, dts.us) |
| 54 | + if tz is not None: |
| 55 | + if tz is pytz.utc: |
| 56 | + for i in range(n): |
| 57 | + pandas_datetime_to_datetimestruct(arr[i], PANDAS_FR_ns, &dts) |
| 58 | + result[i] = datetime(dts.year, dts.month, dts.day, dts.hour, |
| 59 | + dts.min, dts.sec, dts.us, tz) |
| 60 | + else: |
| 61 | + trans = _get_transitions(tz) |
| 62 | + deltas = _get_deltas(tz) |
| 63 | + for i in range(n): |
| 64 | + # Adjust datetime64 timestamp, recompute datetimestruct |
| 65 | + pos = trans.searchsorted(arr[i]) - 1 |
| 66 | + inf = tz._transition_info[pos] |
| 67 | + |
| 68 | + pandas_datetime_to_datetimestruct(arr[i] + deltas[pos], |
| 69 | + PANDAS_FR_ns, &dts) |
| 70 | + result[i] = datetime(dts.year, dts.month, dts.day, dts.hour, |
| 71 | + dts.min, dts.sec, dts.us, |
| 72 | + tz._tzinfos[inf]) |
| 73 | + else: |
| 74 | + for i in range(n): |
| 75 | + pandas_datetime_to_datetimestruct(arr[i], PANDAS_FR_ns, &dts) |
| 76 | + result[i] = datetime(dts.year, dts.month, dts.day, dts.hour, |
| 77 | + dts.min, dts.sec, dts.us) |
58 | 78 |
|
59 | 79 | return result
|
60 | 80 |
|
@@ -163,6 +183,23 @@ class Timestamp(_Timestamp):
|
163 | 183 | return Timestamp(datetime.replace(self, **kwds),
|
164 | 184 | offset=self.offset)
|
165 | 185 |
|
| 186 | + def to_pydatetime(self, warn=True): |
| 187 | + """ |
| 188 | + If warn=True, issue warning if nanoseconds is nonzero |
| 189 | + """ |
| 190 | + cdef: |
| 191 | + pandas_datetimestruct dts |
| 192 | + _TSObject ts |
| 193 | + |
| 194 | + if self.nanosecond != 0 and warn: |
| 195 | + print 'Warning: discarding nonzero nanoseconds' |
| 196 | + ts = convert_to_tsobject(self, self.tzinfo) |
| 197 | + |
| 198 | + return datetime(ts.dts.year, ts.dts.month, ts.dts.day, |
| 199 | + ts.dts.hour, ts.dts.min, ts.dts.sec, |
| 200 | + ts.dts.us, ts.tzinfo) |
| 201 | + |
| 202 | + |
166 | 203 | class NaTType(_NaT):
|
167 | 204 |
|
168 | 205 | def __new__(cls):
|
|
0 commit comments