question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

da.loc[date[0]] brings up two instead of one

See original GitHub issue

Problem description

My dataset has lots of days, with data for two times for each day: midnight and midday (12am and 12pm). With date[0], I am trying to retrieve only the midnight data from the first day; with date[1], the midday data from the same day; date[3] the midnight data from the next day, etc. Now I’m writing this, I’m not sure I’ve posted this in the right place so please let me know if I haven’t.

Code Sample 1: this gives data for midnight and midday (time: 2)

from datetime import datetime, timedelta
start = datetime(1979, 1, 1)
date = [start.strftime('%Y%m%d')]
a = first_dataset.loc[date[0]]
print(a)

<xarray.DataArray (time: 2, latitude: 241, longitude: 480)>
array([[[  #there are numbers here, cut out for ease of reading
            ]]])
Coordinates:
  * longitude  (longitude) float32 0.0 0.75 1.5 2.25 3.0 3.75 4.5 5.25 6.0 ...
  * latitude   (latitude) float32 90.0 89.25 88.5 87.75 87.0 86.25 85.5 ...
  * time       (time) datetime64[ns] 1979-01-01 1979-01-01 T12:00:00 #as you can see here, for
#midnight the time is just 1979-01-01 instead of 1979-01-01 00:00:00, which I think might be causing
#some confusion in my code

Code Sample 2: try adding hour to the date

from datetime import datetime, timedelta
start = datetime(1979, 1, 1)
date = [start.strftime('%Y%m%d%H')] #this what I changed
a = first_dataset.loc[date[0]]
print(a)

Traceback (most recent call last):
  File "pandas/index.pyx", line 589, in pandas.index.DatetimeEngine.get_loc (pandas/index.c:11661)
  File "pandas/src/hashtable_class_helper.pxi", line 404, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8543)
TypeError: an integer is required
 
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/nerc/n02/n02/amethyst/.conda/envs/my_root/lib/python3.4/site-packages/pandas/indexes/base.py", line 2134, in get_loc
    return self._engine.get_loc(key)
  File "pandas/index.pyx", line 553, in pandas.index.DatetimeEngine.get_loc (pandas/index.c:11829)
  File "pandas/index.pyx", line 591, in pandas.index.DatetimeEngine.get_loc (pandas/index.c:11735)
  File "pandas/index.pyx", line 597, in pandas.index.DatetimeEngine._date_check_type (pandas/index.c:11899)
KeyError: '1979010100'

Expected Output

a = first_dataset.loc[date[0]]
print(a)

<xarray.DataArray (time: 1, latitude: 241, longitude: 480)>
array([[[  #there are numbers here, cut out for ease of reading
            ]]])
Coordinates:
  * longitude  (longitude) float32 0.0 0.75 1.5 2.25 3.0 3.75 4.5 5.25 6.0 ...
  * latitude   (latitude) float32 90.0 89.25 88.5 87.75 87.0 86.25 85.5 ...
  * time       (time) datetime64[ns] 1979-01-01

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
am-thystcommented, Oct 2, 2018

@spencerkclark yeah I was using a different platform, updated my xarray version and this works now, thanks!

0reactions
spencerkclarkcommented, Oct 2, 2018

This seems like an xarray version issue; the other argument was not added to where until version 0.10.0. Are you running this script on a different platform than before? I’m surprised your original code, https://github.com/pydata/xarray/issues/2450#issuecomment-426346137, didn’t raise the same error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Commands/locate - Minecraft Wiki - Fandom
Displays the coordinates for the closest configured structure feature and biomes of a given type in the chat for the player who executed...
Read more >
What to do if you get an alert that an AirTag, Find My network ...
Go to Settings > Privacy > Location Services, and turn Location Services on. Go to Settings > Privacy > Location Services > System...
Read more >
Zoom: One platform to connect
One platform to innovate. Bring teams together, reimagine workspaces, engage new audiences, ... Stay up to date on news, learn best practices, and...
Read more >
Manage your Android device's location settings
Manage your Android device's location settings · Understand the location settings available on your phone · Turn location on or off for your...
Read more >
How to use multiple monitors in Windows - Microsoft Support
You'll see this option when Windows detects more than one display. Each display will be numbered to help you identify them more easily....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found