|
12 | 12 | import xarray as xr |
13 | 13 | import copy |
14 | 14 | import warnings |
| 15 | +from pyspedas import is_timezone_aware |
15 | 16 |
|
16 | 17 | tplot_num = 1 |
17 | 18 |
|
@@ -161,11 +162,16 @@ def store_data(name, data=None, delete=False, newname=None, attr_dict={}): |
161 | 162 |
|
162 | 163 | # Convert input time representation to np.datetime64 objects, if needed |
163 | 164 | if isinstance(times, pd.Series): |
164 | | - logging.warning('store_data: unchecked conversion of pd.Series to numpy times') |
165 | | - datetimes = times.to_numpy() # if it is pandas series, convert to numpy array |
| 165 | + datetimes = times.to_numpy(dtype='datetime64[ns]') # if it is pandas series, convert to numpy array |
166 | 166 | elif isinstance(times[0],datetime.datetime): |
167 | 167 | # Timezone-naive datetime, do explicit conversion to np.datetime64[ns] and ensure container is a numpy array |
168 | | - if isinstance(times,np.ndarray): |
| 168 | + if is_timezone_aware(times): |
| 169 | + # Numpy will complain if it is given timezone-aware datetimes to convert. |
| 170 | + # So we convert to UTC first, then drop the timezone entirely |
| 171 | + tz_aware_utc = [aware_dt.astimezone(datetime.timezone.utc) for aware_dt in times] |
| 172 | + tz_naive = [aware_dt.replace(tzinfo=None) for aware_dt in tz_aware_utc] |
| 173 | + datetimes = np.array(tz_naive,dtype='datetime64[ns]') |
| 174 | + elif isinstance(times,np.ndarray): |
169 | 175 | datetimes = times.astype('datetime64[ns]') |
170 | 176 | else: |
171 | 177 | datetimes = np.array(times,dtype='datetime64[ns]') |
@@ -195,8 +201,7 @@ def store_data(name, data=None, delete=False, newname=None, attr_dict={}): |
195 | 201 | datetimes = np.array(times,dtype='datetime64[ns]') |
196 | 202 | else: |
197 | 203 | # Hope it's convertable to a numpy array! This case will get hit for an xarray DataArray. |
198 | | - logging.warning('store_data: unchecked conversion of xarray? to numpy times') |
199 | | - datetimes = np.array(times) |
| 204 | + datetimes = np.array(times).astype('datetime64[ns]') |
200 | 205 |
|
201 | 206 | times = datetimes |
202 | 207 |
|
|
0 commit comments