Skip to content

Commit 449634a

Browse files
committed
Avoid pandas FutureWarning on concatenating empty data frame
See pandas-dev/pandas#52532
1 parent a30a012 commit 449634a

File tree

1 file changed

+60
-51
lines changed

1 file changed

+60
-51
lines changed

qiskit_experiments/test/fake_service.py

+60-51
Original file line numberDiff line numberDiff line change
@@ -111,33 +111,38 @@ def create_experiment(
111111
# backend - the query methods `experiment` and `experiments` are supposed to return an
112112
# an instantiated backend object, and not only the backend name. We assume that the fake
113113
# service works with the fake backend (class FakeBackend).
114-
self.exps = pd.concat(
114+
row = pd.DataFrame(
115115
[
116-
self.exps,
117-
pd.DataFrame(
118-
[
119-
{
120-
"experiment_type": experiment_type,
121-
"experiment_id": experiment_id,
122-
"parent_id": parent_id,
123-
"backend_name": backend_name,
124-
"metadata": metadata,
125-
"job_ids": job_ids,
126-
"tags": tags,
127-
"notes": notes,
128-
"share_level": kwargs.get("share_level", None),
129-
"device_components": [],
130-
"start_datetime": datetime(2022, 1, 1)
131-
+ timedelta(hours=len(self.exps)),
132-
"figure_names": [],
133-
"backend": FakeBackend(backend_name=backend_name),
134-
}
135-
],
136-
columns=self.exps.columns,
137-
),
116+
{
117+
"experiment_type": experiment_type,
118+
"experiment_id": experiment_id,
119+
"parent_id": parent_id,
120+
"backend_name": backend_name,
121+
"metadata": metadata,
122+
"job_ids": job_ids,
123+
"tags": tags,
124+
"notes": notes,
125+
"share_level": kwargs.get("share_level", None),
126+
"device_components": [],
127+
"start_datetime": datetime(2022, 1, 1) + timedelta(hours=len(self.exps)),
128+
"figure_names": [],
129+
"backend": FakeBackend(backend_name=backend_name),
130+
}
138131
],
139-
ignore_index=True,
132+
columns=self.exps.columns,
140133
)
134+
if len(self.exps) > 0:
135+
self.exps = pd.concat(
136+
[
137+
self.exps,
138+
row,
139+
],
140+
ignore_index=True,
141+
)
142+
else:
143+
# Avoid the FutureWarning on concatenating empty DataFrames
144+
# introduced in https://github.com/pandas-dev/pandas/pull/52532
145+
self.exps = row
141146

142147
return experiment_id
143148

@@ -293,35 +298,39 @@ def create_analysis_result(
293298
# `IBMExperimentService.create_analysis_result`. Since `DbExperimentData` does not set it
294299
# via kwargs (as it does with chisq), the user cannot control the time and the service
295300
# alone decides about it. Here we've chosen to set the start date of the experiment.
296-
self.results = pd.concat(
301+
row = pd.DataFrame(
297302
[
298-
self.results,
299-
pd.DataFrame(
300-
[
301-
{
302-
"result_data": result_data,
303-
"result_id": result_id,
304-
"result_type": result_type,
305-
"device_components": device_components,
306-
"experiment_id": experiment_id,
307-
"quality": quality,
308-
"verified": verified,
309-
"tags": tags,
310-
"backend_name": self.exps.loc[self.exps.experiment_id == experiment_id]
311-
.iloc[0]
312-
.backend_name,
313-
"chisq": kwargs.get("chisq", None),
314-
"creation_datetime": self.exps.loc[
315-
self.exps.experiment_id == experiment_id
316-
]
317-
.iloc[0]
318-
.start_datetime,
319-
}
320-
]
321-
),
322-
],
323-
ignore_index=True,
303+
{
304+
"result_data": result_data,
305+
"result_id": result_id,
306+
"result_type": result_type,
307+
"device_components": device_components,
308+
"experiment_id": experiment_id,
309+
"quality": quality,
310+
"verified": verified,
311+
"tags": tags,
312+
"backend_name": self.exps.loc[self.exps.experiment_id == experiment_id]
313+
.iloc[0]
314+
.backend_name,
315+
"chisq": kwargs.get("chisq", None),
316+
"creation_datetime": self.exps.loc[self.exps.experiment_id == experiment_id]
317+
.iloc[0]
318+
.start_datetime,
319+
}
320+
]
324321
)
322+
if len(self.results) > 0:
323+
self.results = pd.concat(
324+
[
325+
self.results,
326+
row,
327+
],
328+
ignore_index=True,
329+
)
330+
else:
331+
# Avoid the FutureWarning on concatenating empty DataFrames
332+
# introduced in https://github.com/pandas-dev/pandas/pull/52532
333+
self.results = row
325334

326335
# a helper method for updating the experiment's device components, see usage below
327336
def add_new_components(expcomps):

0 commit comments

Comments
 (0)