|
1 | | -import pytest |
| 1 | +from datetime import datetime |
| 2 | +from zoneinfo import ZoneInfo |
2 | 3 |
|
3 | | -from pvlib import tools |
4 | 4 | import numpy as np |
5 | | -import pandas as pd |
6 | 5 | from numpy.testing import assert_allclose |
| 6 | +import pandas as pd |
| 7 | +import pytest |
| 8 | + |
| 9 | +from pvlib import location, tools |
7 | 10 |
|
8 | 11 |
|
9 | 12 | @pytest.mark.parametrize('keys, input_dict, expected', [ |
@@ -144,3 +147,134 @@ def test_get_pandas_index(args, args_idx): |
144 | 147 | def test_normalize_max2one(data_in, expected): |
145 | 148 | result = tools.normalize_max2one(data_in) |
146 | 149 | assert_allclose(result, expected) |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.parametrize( |
| 153 | + 'input,expected', |
| 154 | + [ |
| 155 | + ( |
| 156 | + { |
| 157 | + "time": datetime( |
| 158 | + 1974, 6, 22, 18, 30, 15, tzinfo=ZoneInfo("Etc/GMT+5"), |
| 159 | + ), |
| 160 | + "location": location.Location( |
| 161 | + 43.19262774396091, -77.58782907414867, tz="Etc/GMT+5" |
| 162 | + ) |
| 163 | + }, |
| 164 | + datetime(1974, 6, 22, 23, 30, 15, tzinfo=ZoneInfo("UTC")), |
| 165 | + ), |
| 166 | + ( |
| 167 | + { |
| 168 | + "time": datetime(1974, 6, 22, 18, 30, 15), |
| 169 | + "location": location.Location( |
| 170 | + 43.19262774396091, -77.58782907414867, tz="Etc/GMT+5" |
| 171 | + ) |
| 172 | + }, |
| 173 | + datetime(1974, 6, 22, 23, 30, 15, tzinfo=ZoneInfo("UTC")), |
| 174 | + ), |
| 175 | + ( |
| 176 | + { |
| 177 | + "time": pd.DatetimeIndex( |
| 178 | + ["1974-06-22T18:30:15"], |
| 179 | + tz=ZoneInfo("Etc/GMT+5"), |
| 180 | + ), |
| 181 | + "location": location.Location( |
| 182 | + 43.19262774396091, -77.58782907414867, tz="Etc/GMT+5" |
| 183 | + ) |
| 184 | + }, |
| 185 | + pd.DatetimeIndex(["1974-06-22T23:30:15"], tz=ZoneInfo("UTC")), |
| 186 | + ), |
| 187 | + ( |
| 188 | + { |
| 189 | + "time": pd.DatetimeIndex(["1974-06-22T18:30:15"]), |
| 190 | + "location": location.Location( |
| 191 | + 43.19262774396091, -77.58782907414867, tz="Etc/GMT+5" |
| 192 | + ) |
| 193 | + }, |
| 194 | + pd.DatetimeIndex(["1974-06-22T23:30:15"], tz=ZoneInfo("UTC")), |
| 195 | + ), |
| 196 | + ( |
| 197 | + { |
| 198 | + "time": pd.Series( |
| 199 | + [24.42], |
| 200 | + index=pd.DatetimeIndex( |
| 201 | + ["1974-06-22T18:30:15"], |
| 202 | + tz=ZoneInfo("Etc/GMT+5"), |
| 203 | + ), |
| 204 | + ), |
| 205 | + "location": location.Location( |
| 206 | + 43.19262774396091, -77.58782907414867, tz="Etc/GMT+5" |
| 207 | + ) |
| 208 | + }, |
| 209 | + pd.Series( |
| 210 | + [24.42], |
| 211 | + pd.DatetimeIndex(["1974-06-22T23:30:15"], tz=ZoneInfo("UTC")), |
| 212 | + ), |
| 213 | + ), |
| 214 | + ( |
| 215 | + { |
| 216 | + "time": pd.Series( |
| 217 | + [24.42], |
| 218 | + index=pd.DatetimeIndex(["1974-06-22T18:30:15"]), |
| 219 | + ), |
| 220 | + "location": location.Location( |
| 221 | + 43.19262774396091, -77.58782907414867, tz="Etc/GMT+5" |
| 222 | + ) |
| 223 | + }, |
| 224 | + pd.Series( |
| 225 | + [24.42], |
| 226 | + pd.DatetimeIndex(["1974-06-22T23:30:15"], tz=ZoneInfo("UTC")), |
| 227 | + ), |
| 228 | + ), |
| 229 | + ( |
| 230 | + { |
| 231 | + "time": pd.DataFrame( |
| 232 | + [[24.42]], |
| 233 | + index=pd.DatetimeIndex( |
| 234 | + ["1974-06-22T18:30:15"], |
| 235 | + tz=ZoneInfo("Etc/GMT+5"), |
| 236 | + ), |
| 237 | + ), |
| 238 | + "location": location.Location( |
| 239 | + 43.19262774396091, -77.58782907414867, tz="Etc/GMT+5" |
| 240 | + ) |
| 241 | + }, |
| 242 | + pd.DataFrame( |
| 243 | + [[24.42]], |
| 244 | + pd.DatetimeIndex(["1974-06-22T23:30:15"], tz=ZoneInfo("UTC")), |
| 245 | + ), |
| 246 | + ), |
| 247 | + ( |
| 248 | + { |
| 249 | + "time": pd.DataFrame( |
| 250 | + [[24.42]], |
| 251 | + index=pd.DatetimeIndex(["1974-06-22T18:30:15"]), |
| 252 | + ), |
| 253 | + "location": location.Location( |
| 254 | + 43.19262774396091, -77.58782907414867, tz="Etc/GMT+5" |
| 255 | + ) |
| 256 | + }, |
| 257 | + pd.DataFrame( |
| 258 | + [[24.42]], |
| 259 | + pd.DatetimeIndex(["1974-06-22T23:30:15"], tz=ZoneInfo("UTC")), |
| 260 | + ), |
| 261 | + ), |
| 262 | + ], |
| 263 | + ids=[ |
| 264 | + "datetime.datetime with tzinfo", |
| 265 | + "datetime.datetime", |
| 266 | + "pandas.DatetimeIndex with tzinfo", |
| 267 | + "pandas.DatetimeIndex", |
| 268 | + "pandas.Series with tzinfo", |
| 269 | + "pandas.Series", |
| 270 | + "pandas.DataFrame with tzinfo", |
| 271 | + "pandas.DataFrame", |
| 272 | + ], |
| 273 | +) |
| 274 | +def test_localize_to_utc(input, expected): |
| 275 | + """Test localization of naive time to UTC using the specified location.""" |
| 276 | + got = tools.localize_to_utc(**input) |
| 277 | + if isinstance(got, (pd.Series, pd.DataFrame)): |
| 278 | + assert got.equals(expected) |
| 279 | + else: |
| 280 | + assert got == expected |
0 commit comments