Skip to content

Commit d11bb17

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

File tree

1 file changed

+62
-50
lines changed

1 file changed

+62
-50
lines changed

qiskit_experiments/test/fake_service.py

+62-50
Original file line numberDiff line numberDiff line change
@@ -111,33 +111,39 @@ 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)
128+
+ timedelta(hours=len(self.exps)),
129+
"figure_names": [],
130+
"backend": FakeBackend(backend_name=backend_name),
131+
}
138132
],
139-
ignore_index=True,
133+
columns=self.exps.columns,
140134
)
135+
if len(self.exps) > 0:
136+
self.exps = pd.concat(
137+
[
138+
self.exps,
139+
row,
140+
],
141+
ignore_index=True,
142+
)
143+
else:
144+
# Avoid the FutureWarning on concatenating empty DataFrames
145+
# introduced in https://github.com/pandas-dev/pandas/pull/52532
146+
self.exps = row
141147

142148
return experiment_id
143149

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

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

0 commit comments

Comments
 (0)