Commit 59f57f3
Fix indexing with datetime64[ns] with pandas=1.1 (#4292)
* Fix indexing with datetime64[ns] with pandas=1.1
Fixes #4283
The underlying issue is that calling `.item()` on a NumPy array with
`dtype=datetime64[ns]` returns an _integer_, rather than an `np.datetime64
scalar. This is somewhat baffling but works this way because `.item()`
returns native Python types, but `datetime.datetime` doesn't support
nanosecond precision.
`pandas.Index.get_loc` used to support these integers, but now is more strict.
Hence we get errors.
We can fix this by using `array[()]` to convert 0d arrays into NumPy scalars
instead of calling `array.item()`.
I've added a crude regression test. There may well be a better way to test this
but I haven't figured it out yet.
* lint fix
* add a test checking the datetime indexer
* use label.item() for non-datetime / timedelta labels
* unpin pandas in the docs
* ignore the future warning about deprecated arguments to pandas.Grouper
* Update xarray/core/indexing.py
Co-authored-by: keewis <[email protected]>
* Add whatsnew note
Co-authored-by: Keewis <[email protected]>
Co-authored-by: Maximilian Roos <[email protected]>
Co-authored-by: keewis <[email protected]>1 parent 66ab0ae commit 59f57f3
File tree
6 files changed
+40
-16
lines changed- ci/requirements
- doc
- xarray
- core
- tests
6 files changed
+40
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
| 21 | + | |
24 | 22 | | |
25 | 23 | | |
26 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
89 | 92 | | |
90 | 93 | | |
91 | 94 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1126 | 1126 | | |
1127 | 1127 | | |
1128 | 1128 | | |
1129 | | - | |
1130 | | - | |
1131 | | - | |
1132 | | - | |
1133 | | - | |
1134 | | - | |
1135 | | - | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
1136 | 1135 | | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
1137 | 1145 | | |
1138 | 1146 | | |
1139 | 1147 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
| 181 | + | |
| 182 | + | |
181 | 183 | | |
182 | | - | |
| 184 | + | |
183 | 185 | | |
184 | 186 | | |
185 | 187 | | |
| |||
189 | 191 | | |
190 | 192 | | |
191 | 193 | | |
192 | | - | |
| 194 | + | |
193 | 195 | | |
194 | | - | |
195 | | - | |
196 | | - | |
| 196 | + | |
197 | 197 | | |
198 | 198 | | |
199 | 199 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
938 | 938 | | |
939 | 939 | | |
940 | 940 | | |
941 | | - | |
| 941 | + | |
942 | 942 | | |
943 | 943 | | |
944 | 944 | | |
| |||
1078 | 1078 | | |
1079 | 1079 | | |
1080 | 1080 | | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
1081 | 1087 | | |
1082 | 1088 | | |
1083 | 1089 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
89 | 98 | | |
90 | 99 | | |
91 | 100 | | |
| |||
0 commit comments