Skip to content

Commit

Permalink
remove pandas deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann committed Sep 12, 2023
1 parent 9d9121d commit a35878b
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pyam/iiasa.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from io import StringIO
from pathlib import Path
import json
import logging
Expand Down Expand Up @@ -242,7 +243,7 @@ def meta_columns(self):
url = "/".join([self._base_url, "metadata/types"])
r = requests.get(url, headers=self.auth())
_check_response(r)
return pd.read_json(r.text, orient="records")["name"]
return pd.read_json(StringIO(r.text), orient="records")["name"]

def _query_index(self, default_only=True, meta=False, cols=[], **kwargs):
# TODO: at present this reads in all data for all scenarios,
Expand All @@ -255,7 +256,7 @@ def _query_index(self, default_only=True, meta=False, cols=[], **kwargs):
_check_response(r)

# cast response to dataframe, apply filter by kwargs, and return
runs = pd.read_json(r.text, orient="records")
runs = pd.read_json(StringIO(r.text), orient="records")
if runs.empty:
logger.warning("No permission to view model(s) or no scenarios exist.")
return pd.DataFrame([], columns=META_IDX + ["version", "run_id"] + cols)
Expand Down Expand Up @@ -360,7 +361,7 @@ def variables(self):
url = "/".join([self._base_url, "ts"])
r = requests.get(url, headers=self.auth())
_check_response(r)
df = pd.read_json(r.text, orient="records")
df = pd.read_json(StringIO(r.text), orient="records")
return pd.Series(df["variable"].unique(), name="variable")

@lru_cache()
Expand All @@ -382,7 +383,7 @@ def regions(self, include_synonyms=False):

@staticmethod
def convert_regions_payload(response, include_synonyms):
df = pd.read_json(response, orient="records")
df = pd.read_json(StringIO(response), orient="records")
if df.empty:
return df
if "synonyms" not in df.columns:
Expand Down Expand Up @@ -449,10 +450,6 @@ def _match(data, patterns):
# pass empty list to API if all regions selected
if len(regions) == len(self.regions()):
regions = []
logger.debug(
f"Prepared filter for {len(regions)} region(s), "
f"{len(variables)} variables and {len(runs)} runs"
)
data = {
"filters": {
"regions": list(regions),
Expand Down Expand Up @@ -523,7 +520,6 @@ def query(self, default_only=True, meta=True, **kwargs):
# retrieve data
_args = json.dumps(self._query_post(_meta, default_only=default_only, **kwargs))
url = "/".join([self._base_url, "runs/bulk/ts"])
logger.debug(f"Query timeseries data from {url} with data {_args}")
r = requests.post(url, headers=headers, data=_args)
_check_response(r)
# refactor returned json object to be castable to an IamDataFrame
Expand All @@ -537,8 +533,7 @@ def query(self, default_only=True, meta=True, **kwargs):
value=float,
version=int,
)
data = pd.read_json(r.text, orient="records", dtype=dtype)
logger.debug(f"Response: {len(r.text)} bytes, {len(data)} records")
data = pd.read_json(StringIO(r.text), orient="records", dtype=dtype)
cols = IAMC_IDX + ["year", "value", "subannual", "version"]
# keep only known columns or init empty df
data = pd.DataFrame(data=data, columns=cols)
Expand Down

0 comments on commit a35878b

Please sign in to comment.