diff --git a/extra_data/keydata.py b/extra_data/keydata.py
index 858fb96..5e5573e 100644
--- a/extra_data/keydata.py
+++ b/extra_data/keydata.py
@@ -95,6 +95,27 @@ class KeyData:
     def __getitem__(self, item):
         return self.select_trains(item)
 
+    def data_counts(self):
+        """Get a count of data points in each train.
+
+        Returns a pandas series with an index of train IDs.
+        """
+        import pandas as pd
+
+        seq_series = []
+
+        for f in self.files:
+            if self.section == 'CONTROL':
+                counts = np.ones(len(f.train_ids), dtype=np.uint64)
+            else:
+                _, counts = f.get_index(self.source, self._key_group)
+            seq_series.append(pd.Series(counts, index=f.train_ids))
+
+        ser = pd.concat(sorted(seq_series, key=lambda s: s.index[0]))
+        # Select out only the train IDs of interest
+        train_ids = ser.index.intersection(self.train_ids)
+        return ser.loc[train_ids]
+
     # Getting data as different kinds of array: -------------------------------
 
     def ndarray(self, roi=()):
diff --git a/extra_data/reader.py b/extra_data/reader.py
index fe1f038..c22a210 100644
--- a/extra_data/reader.py
+++ b/extra_data/reader.py
@@ -409,23 +409,7 @@ class DataCollection:
         key: str
             Key of parameter within that device, e.g. "image.data".
         """
-        import pandas as pd
-
-        self._check_field(source, key)
-        seq_series = []
-
-        for f in self._source_index[source]:
-            if source in self.control_sources:
-                counts = np.ones(len(f.train_ids), dtype=np.uint64)
-            else:
-                group = key.partition('.')[0]
-                _, counts = f.get_index(source, group)
-            seq_series.append(pd.Series(counts, index=f.train_ids))
-
-        ser = pd.concat(sorted(seq_series, key=lambda s: s.index[0]))
-        # Select out only the train IDs of interest
-        train_ids = ser.index.intersection(self.train_ids)
-        return ser.loc[train_ids]
+        return self._get_key_data(source, key).data_counts()
 
     def get_series(self, source, key):
         """Return a pandas Series for a particular data field.

train_ids in KeyData
the `train_ids` attribute of the `KeyData` object is a simple copy of the train ids contained in the `DataCollection`. That leads to some inconsistencies. For example calling `.shape` will return a shape where the trainId axis match the actual count of data available in the files for the source, but calling `.train_ids` will contains train ids with and without data for this key.

One way to go around this if for example using xarray: `data.xarray().trainIds` will return only the train ids containing data, but that'd be ideal if it wasn't necessary to load the data to find which trainIds have or do not have data.

Would it be sensible to have the `train_ids` filtered (either at init or when the property is called) and only contains the train ids that actually have data for the key?
diff --git a/extra_data/tests/make_examples.py b/extra_data/tests/make_examples.py
index b3fde71..3d7625a 100644
--- a/extra_data/tests/make_examples.py
+++ b/extra_data/tests/make_examples.py
@@ -269,8 +269,7 @@ def make_reduced_spb_run(dir_path, raw=True, rng=None, format_version='0.5'):
             AGIPDModule('SPB_DET_AGIPD1M-1/DET/{}CH0'.format(modno), raw=raw,
                          frames_per_train=frame_counts)
             ], ntrains=64, chunksize=32, format_version=format_version)
-    if not raw:
-        return
+
     write_file(osp.join(dir_path, '{}-R0238-DA01-S00000.h5'.format(prefix)),
                [ XGM('SA1_XTD2_XGM/DOOCS/MAIN'),
                  XGM('SPB_XTD9_XGM/DOOCS/MAIN'),
diff --git a/extra_data/tests/test_keydata.py b/extra_data/tests/test_keydata.py
index dc2657b..7edf40f 100644
--- a/extra_data/tests/test_keydata.py
+++ b/extra_data/tests/test_keydata.py
@@ -84,3 +84,23 @@ def test_get_train(mock_spb_raw_run):
 
     with pytest.raises(IndexError):
         xgm_beam_x.train_from_index(9999)
+
+
+def test_data_counts(mock_reduced_spb_proc_run):
+    run = RunDirectory(mock_reduced_spb_proc_run)
+
+    # control data
+    xgm_beam_x = run['SPB_XTD9_XGM/DOOCS/MAIN', 'beamPosition.ixPos.value']
+    count = xgm_beam_x.data_counts()
+    assert count.index.tolist() == xgm_beam_x.train_ids
+    assert (count.values == 1).all()
+
+    # intrument data
+    camera = run['SPB_IRU_CAM/CAM/SIDEMIC:daqOutput', 'data.image.pixels']
+    count = camera.data_counts()
+    assert count.index.tolist() == camera.train_ids
+
+    mod = run['SPB_DET_AGIPD1M-1/DET/0CH0:xtdf', 'image.data']
+    count = mod.data_counts()
+    assert count.index.tolist() == mod.train_ids
+    assert count.values.sum() == mod.shape[0]
diff --git a/extra_data/tests/test_reader_mockdata.py b/extra_data/tests/test_reader_mockdata.py
index 730e6d0..1000570 100644
--- a/extra_data/tests/test_reader_mockdata.py
+++ b/extra_data/tests/test_reader_mockdata.py
@@ -636,3 +636,10 @@ def test_permission():
 def test_empty_file_info(mock_empty_file, capsys):
     f = H5File(mock_empty_file)
     f.info()  # smoke test
+
+
+def test_get_data_counts(mock_spb_raw_run):
+    run = RunDirectory(mock_spb_raw_run)
+    count = run.get_data_counts('SPB_XTD9_XGM/DOOCS/MAIN', 'beamPosition.ixPos.value')
+    assert count.index.tolist() == run.train_ids
+    assert (count.values == 1).all()

pip install -e .[test]
parse_log_pytest
requirements.txt
pytest
3.7
requirements/base.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==24.2.0
backcall==0.2.0
certifi @ file:///croot/certifi_1671487769961/work/certifi
cloudpickle==2.2.1
coverage==7.2.7
cycler==0.11.0
dask==2022.2.0
debugpy==1.7.0
decorator==5.1.1
entrypoints==0.4
exceptiongroup==1.2.2
-e git+https://github.com/European-XFEL/EXtra-data.git@2b3fe44a66e4cdf49323441add35bd633c8bc192#egg=EXtra_data
fabio==2024.9.0
fastjsonschema==2.21.1
fonttools==4.38.0
fsspec==2023.1.0
h5py==3.8.0
hdf5plugin==4.4.0
importlib-metadata==6.7.0
importlib-resources==5.12.0
iniconfig==2.0.0
ipykernel==6.16.2
ipython==7.34.0
jedi==0.19.2
jsonschema==4.17.3
jupyter_client==7.4.9
jupyter_core==4.12.0
karabo-bridge==0.7.0
kiwisolver==1.4.5
locket==1.0.0
lxml==5.3.1
matplotlib==3.5.3
matplotlib-inline==0.1.6
msgpack==1.0.5
msgpack-numpy==0.4.8
nbformat==5.8.0
nbval==0.11.0
nest-asyncio==1.6.0
numpy==1.21.6
packaging==24.0
pandas==1.3.5
parso==0.8.4
partd==1.4.1
pexpect==4.9.0
pickleshare==0.7.5
Pillow==9.5.0
pkgutil_resolve_name==1.3.10
pluggy==1.2.0
prompt_toolkit==3.0.48
psutil==7.0.0
ptyprocess==0.7.0
Pygments==2.17.2
pyparsing==3.1.4
pyrsistent==0.19.3
pytest==7.4.4
pytest-cov==4.1.0
python-dateutil==2.9.0.post0
pytz==2025.2
PyYAML==6.0.1
pyzmq==26.2.1
scipy==1.7.3
six==1.17.0
testpath==0.6.0
tomli==2.0.1
toolz==0.12.1
tornado==6.2
traitlets==5.9.0
typing_extensions==4.7.1
wcwidth==0.2.13
xarray==0.20.2
zipp==3.15.0

name: EXtra-data
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==24.2.0
      - backcall==0.2.0
      - cloudpickle==2.2.1
      - coverage==7.2.7
      - cycler==0.11.0
      - dask==2022.2.0
      - debugpy==1.7.0
      - decorator==5.1.1
      - entrypoints==0.4
      - exceptiongroup==1.2.2
      - fabio==2024.9.0
      - fastjsonschema==2.21.1
      - fonttools==4.38.0
      - fsspec==2023.1.0
      - h5py==3.8.0
      - hdf5plugin==4.4.0
      - importlib-metadata==6.7.0
      - importlib-resources==5.12.0
      - iniconfig==2.0.0
      - ipykernel==6.16.2
      - ipython==7.34.0
      - jedi==0.19.2
      - jsonschema==4.17.3
      - jupyter-client==7.4.9
      - jupyter-core==4.12.0
      - karabo-bridge==0.7.0
      - kiwisolver==1.4.5
      - locket==1.0.0
      - lxml==5.3.1
      - matplotlib==3.5.3
      - matplotlib-inline==0.1.6
      - msgpack==1.0.5
      - msgpack-numpy==0.4.8
      - nbformat==5.8.0
      - nbval==0.11.0
      - nest-asyncio==1.6.0
      - numpy==1.21.6
      - packaging==24.0
      - pandas==1.3.5
      - parso==0.8.4
      - partd==1.4.1
      - pexpect==4.9.0
      - pickleshare==0.7.5
      - pillow==9.5.0
      - pkgutil-resolve-name==1.3.10
      - pluggy==1.2.0
      - prompt-toolkit==3.0.48
      - psutil==7.0.0
      - ptyprocess==0.7.0
      - pygments==2.17.2
      - pyparsing==3.1.4
      - pyrsistent==0.19.3
      - pytest==7.4.4
      - pytest-cov==4.1.0
      - python-dateutil==2.9.0.post0
      - pytz==2025.2
      - pyyaml==6.0.1
      - pyzmq==26.2.1
      - scipy==1.7.3
      - six==1.17.0
      - testpath==0.6.0
      - tomli==2.0.1
      - toolz==0.12.1
      - tornado==6.2
      - traitlets==5.9.0
      - typing-extensions==4.7.1
      - wcwidth==0.2.13
      - xarray==0.20.2
      - zipp==3.15.0
prefix: /opt/conda/envs/EXtra-data

extra_data/tests/test_keydata.py::test_data_counts[0.5] extra_data/tests/test_keydata.py::test_data_counts[1.0]
extra_data/tests/test_keydata.py::test_get_keydata[0.5] extra_data/tests/test_keydata.py::test_select_trains[0.5] extra_data/tests/test_keydata.py::test_nodata[0.5] extra_data/tests/test_keydata.py::test_iter_trains[0.5] extra_data/tests/test_keydata.py::test_get_train[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_trains[0.5] extra_data/tests/test_reader_mockdata.py::test_get_train_bad_device_name[0.5] extra_data/tests/test_reader_mockdata.py::test_detector_info_oldfmt[0.5] extra_data/tests/test_reader_mockdata.py::test_detector_info[0.5] extra_data/tests/test_reader_mockdata.py::test_train_info[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_trains_fxe[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_file_select_trains[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_trains_select_keys[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_trains_require_all[0.5] extra_data/tests/test_reader_mockdata.py::test_read_fxe_raw_run[0.5] extra_data/tests/test_reader_mockdata.py::test_read_fxe_raw_run_selective[0.5] extra_data/tests/test_reader_mockdata.py::test_read_spb_proc_run[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_spb_raw_run[0.5] extra_data/tests/test_reader_mockdata.py::test_properties_fxe_raw_run[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_fxe_run[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_select_trains[0.5] extra_data/tests/test_reader_mockdata.py::test_iterate_run_glob_devices[0.5] extra_data/tests/test_reader_mockdata.py::test_train_by_id_fxe_run[0.5] extra_data/tests/test_reader_mockdata.py::test_train_by_id_fxe_run_selection[0.5] extra_data/tests/test_reader_mockdata.py::test_train_from_index_fxe_run[0.5] extra_data/tests/test_reader_mockdata.py::test_file_get_series_control[0.5] extra_data/tests/test_reader_mockdata.py::test_file_get_series_instrument[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_series_control[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_series_select_trains[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_dataframe[0.5] extra_data/tests/test_reader_mockdata.py::test_file_get_array[0.5] extra_data/tests/test_reader_mockdata.py::test_file_get_array_missing_trains[0.5] extra_data/tests/test_reader_mockdata.py::test_file_get_array_control_roi[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_array[0.5-defaultName] extra_data/tests/test_reader_mockdata.py::test_run_get_array[0.5-explicitName] extra_data/tests/test_reader_mockdata.py::test_run_get_array_empty[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_array_error[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_array_select_trains[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_array_roi[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_array_multiple_per_train[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_virtual_dataset[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_virtual_dataset_filename[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_dask_array[0.5] extra_data/tests/test_reader_mockdata.py::test_run_get_dask_array_labelled[0.5] extra_data/tests/test_reader_mockdata.py::test_select[0.5] extra_data/tests/test_reader_mockdata.py::test_deselect[0.5] extra_data/tests/test_reader_mockdata.py::test_select_trains[0.5] extra_data/tests/test_reader_mockdata.py::test_union[0.5] extra_data/tests/test_reader_mockdata.py::test_union_raw_proc[0.5] extra_data/tests/test_reader_mockdata.py::test_read_skip_invalid[0.5] extra_data/tests/test_reader_mockdata.py::test_run_immutable_sources[0.5] extra_data/tests/test_reader_mockdata.py::test_open_run[0.5] extra_data/tests/test_reader_mockdata.py::test_open_file[0.5] extra_data/tests/test_reader_mockdata.py::test_get_data_counts[0.5] extra_data/tests/test_keydata.py::test_get_keydata[1.0] extra_data/tests/test_keydata.py::test_select_trains[1.0] extra_data/tests/test_keydata.py::test_nodata[1.0] extra_data/tests/test_keydata.py::test_iter_trains[1.0] extra_data/tests/test_keydata.py::test_get_train[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_trains[1.0] extra_data/tests/test_reader_mockdata.py::test_get_train_bad_device_name[1.0] extra_data/tests/test_reader_mockdata.py::test_detector_info_oldfmt[1.0] extra_data/tests/test_reader_mockdata.py::test_detector_info[1.0] extra_data/tests/test_reader_mockdata.py::test_train_info[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_trains_fxe[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_file_select_trains[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_trains_select_keys[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_trains_require_all[1.0] extra_data/tests/test_reader_mockdata.py::test_read_fxe_raw_run[1.0] extra_data/tests/test_reader_mockdata.py::test_read_fxe_raw_run_selective[1.0] extra_data/tests/test_reader_mockdata.py::test_read_spb_proc_run[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_spb_raw_run[1.0] extra_data/tests/test_reader_mockdata.py::test_properties_fxe_raw_run[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_fxe_run[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_select_trains[1.0] extra_data/tests/test_reader_mockdata.py::test_iterate_run_glob_devices[1.0] extra_data/tests/test_reader_mockdata.py::test_train_by_id_fxe_run[1.0] extra_data/tests/test_reader_mockdata.py::test_train_by_id_fxe_run_selection[1.0] extra_data/tests/test_reader_mockdata.py::test_train_from_index_fxe_run[1.0] extra_data/tests/test_reader_mockdata.py::test_file_get_series_control[1.0] extra_data/tests/test_reader_mockdata.py::test_file_get_series_instrument[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_series_control[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_series_select_trains[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_dataframe[1.0] extra_data/tests/test_reader_mockdata.py::test_file_get_array[1.0] extra_data/tests/test_reader_mockdata.py::test_file_get_array_missing_trains[1.0] extra_data/tests/test_reader_mockdata.py::test_file_get_array_control_roi[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_array[1.0-defaultName] extra_data/tests/test_reader_mockdata.py::test_run_get_array[1.0-explicitName] extra_data/tests/test_reader_mockdata.py::test_run_get_array_empty[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_array_error[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_array_select_trains[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_array_roi[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_array_multiple_per_train[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_virtual_dataset[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_virtual_dataset_filename[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_dask_array[1.0] extra_data/tests/test_reader_mockdata.py::test_run_get_dask_array_labelled[1.0] extra_data/tests/test_reader_mockdata.py::test_select[1.0] extra_data/tests/test_reader_mockdata.py::test_deselect[1.0] extra_data/tests/test_reader_mockdata.py::test_select_trains[1.0] extra_data/tests/test_reader_mockdata.py::test_union[1.0] extra_data/tests/test_reader_mockdata.py::test_union_raw_proc[1.0] extra_data/tests/test_reader_mockdata.py::test_read_skip_invalid[1.0] extra_data/tests/test_reader_mockdata.py::test_run_immutable_sources[1.0] extra_data/tests/test_reader_mockdata.py::test_open_run[1.0] extra_data/tests/test_reader_mockdata.py::test_open_file[1.0] extra_data/tests/test_reader_mockdata.py::test_get_data_counts[1.0] extra_data/tests/test_reader_mockdata.py::test_empty_file_info
extra_data/keydata.py extra_data/reader.py
extra_data/keydata.py extra_data/reader.py