Skip to content

Commit 44f736f

Browse files
Fix dataframe slicing when start or end are not provided
1 parent ce5e5f9 commit 44f736f

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

datareservoirio/client.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ def get(
395395
pandas.Series
396396
Series data
397397
"""
398+
start_provided = start is not None
399+
end_provided = end is not None
400+
398401
if not start:
399402
start = _START_DEFAULT
400403
if not end:
@@ -430,21 +433,19 @@ def get(
430433
else:
431434
df = pd.DataFrame(columns=("index", "values")).astype({"index": "int64"})
432435

433-
try:
434-
series = (
435-
df.set_index("index").squeeze("columns").loc[start:end].copy(deep=True)
436-
)
437-
except KeyError as e:
436+
s = df.set_index("index").squeeze("columns")
437+
438+
# Ensure sorted (cheap if already sorted)
439+
if not s.index.is_monotonic_increasing:
438440
logging.warning(
439441
"The time series you requested is not properly ordered. The data will be sorted to attempt to resolve the issue. Please note that this operation may take some time."
440442
)
441-
series = (
442-
df.set_index("index")
443-
.sort_index()
444-
.squeeze("columns")
445-
.loc[start:end]
446-
.copy(deep=True)
447-
)
443+
s = s.sort_index()
444+
445+
series = s.loc[
446+
start if start_provided else None : end if end_provided else None
447+
].copy(deep=True)
448+
448449
series.index.name = None
449450

450451
if series.empty and raise_empty: # may become empty after slicing

0 commit comments

Comments
 (0)