Skip to content

dataset

pyDataverse.dataverse.dataset

A Dataverse Dataset object, representing the core container of data, metadata, and files within a Dataverse instance.

This class provides a convenient interface for interacting with Dataverse datasets, including reading and updating metadata, accessing files in the dataset, and supporting read operations for tabular data using pandas.

Attributes

NameTypeDescription
identifierOptional[str]The unique identifier (such as DOI or database ID) for the dataset.
licenseUnion[str, info.License, None]The assigned license for the dataset, as a string or License object.
metadata_blocksDict[str, MetadataBlockBase]All metadata blocks associated with the dataset, keyed by block name.
dataverseDataverseThe parent Dataverse object this dataset belongs to.

Attributes

NameTypeDescription
is_lockedboolCheck if the dataset is locked.
is_in_reviewboolCheck if the dataset is in review.
locksList[locks.Lock]Get the locks of the dataset.
urlstrGet the URL of the dataset.
titlestrGet the title of the dataset from the citation metadata block.
descriptionstrRetrieve the dataset’s description from the citation block.
authorsList[Dict[str, Any]]Get the authors of the dataset from the citation metadata block.
subjectsList[str]Get the subjects of the dataset from the citation metadata block.
fsDataverseFSGet a file system interface (DataverseFS) for this dataset to access its files.
filesObtain a view of files in the dataset, with both iterator and dict-like access.
tabular_filesGet a view of tabular files in the dataset.
json_schemaDict[str, Any]Generate a JSON schema validating all metadata blocks of the dataset.

Methods

from_doi_url(cls, doi_url: str) -> Tuple[Dataverse, Dataset]

Create a Dataset instance from a DOI URL.

This method follows the DOI URL to extract the base Dataverse URL and persistent identifier, then creates both a Dataverse instance and fetches the corresponding Dataset.

Parameters

NameTypeDescription
doi_urlstrThe DOI URL pointing to a dataset (e.g., “https://doi.org/10.18419/DARUS-5539”)

Returns

TypeDescription
Tuple[Dataverse, Dataset]Tuple[Dataverse, Dataset]: A tuple containing the Dataverse instance and the Dataset object

Raises

ExceptionDescription
ValueErrorIf the URL doesn’t lead to a valid Dataverse instance or if the persistent identifier cannot be extracted from the URL parameters
httpx.HTTPStatusErrorIf the HTTP request to the DOI URL fails

Example

>>> dataverse, dataset = Dataset.from_doi_url(“https://doi.org/10.18419/DARUS-5539”) >>> print(dataset.persistent_identifier) doi:10.18419/DARUS-5539

wait_for_unlock(self) -> None

Wait for the dataset to be unlocked.

refresh(self, version: Union[Literal[':latest', ':latest-published', ':draft'], str] = ':latest') -> Dataset

Refresh the dataset from the Dataverse server.

checkout(self, version: Union[Literal[':latest', ':latest-published', ':draft'], str] = ':latest') -> Dataset

Checkout the dataset to the specified version.

open_in_browser(self) -> None

Open the dataset in your system’s default web browser.

This method constructs the dataset’s persistent URL and launches it using Python’s webbrowser module.

Example

>>> dataset.open_in_browser()

Raises

ExceptionDescription
ValueErrorIf the dataset identifier is not set.
dict(self, include: Optional[Sequence[str]] = None) -> Dict[str, Any]

Generate a dictionary representation of this Dataset, omitting fields that are unset.

Parameters

NameTypeDescription
includeOptional[Sequence[str]]Optional list of metadata block names to include. If not specified, all metadata blocks will be included. (default: None)

Returns

TypeDescription
Dict[str, Any]Dict[str, Any]: A dict containing dataset fields with non-None values.
export(self, format: Union[str, SemanticApiFormat]) -> Union[str, Dict[str, Any]]

Export the dataset’s metadata in the specified format via the Dataverse API.

Parameters

NameTypeDescription
formatstrThe export format (e.g., ‘dataverse_json’, ‘DublinCore’, etc.)

Returns

TypeDescription
Union[str, Dict[str, Any]]Union[str, Dict[str, Any]]: The exported metadata, as a string or parsed dict.
graph(self, format: GraphFormat) -> Graph

Get the RDF graph of the dataset by exporting metadata in semantic formats.

This method retrieves the dataset’s metadata in one or more semantic formats (such as JSON-LD, RDF/XML, Turtle, etc.) and converts them into an RDF graph using rdflib. The method validates that the requested formats are available and support semantic data representation.

Parameters

NameTypeDescription
formatGraphFormatA single format string or list of format strings specifying the semantic export formats to use. Valid formats are those available in the Dataverse instance that have semantic media types.

Returns

TypeDescription
GraphAn rdflib Graph object containing the merged RDF triples from all requested export formats.

Raises

ExceptionDescription
ValueErrorIf any of the requested formats are not available in the Dataverse instance or do not support semantic data representation.

Examples

>>> # Get graph using a single format
>>> graph = dataset.graph("OAI_ORE")
>>> # Get graph using multiple formats
>>> graph = dataset.graph(["OAI_ORE", "JSON-LD"])
>>> # Query the resulting graph
>>> for subj, pred, obj in graph:
... print(f"{subj} {pred} {obj}")
serialize_metadata_blocks(self, metadata_blocks: Dict[str, MetadataBlockBase]) -> Dict[str, Any]

Serialize non-empty metadata blocks for this dataset for export or storage.

Parameters

NameTypeDescription
metadata_blocksDict[str, MetadataBlockBase]Map of block names to block objects.

Returns

TypeDescription
Dict[str, Any]Dict[str, Any]: Dictionary mapping block names to their serializable form, excluding empty blocks.
validate_license(cls, value: Union[info.License, str]) -> Optional[str]

Converts a License object into its string name, or returns the string if already provided.

Parameters

NameTypeDescription
valueUnion[info.License, str, None]License value to validate/convert.

Returns

TypeDescription
Optional[str]Optional[str]: The license name as a string, or None if not set.

Example

>>> dataset.license = License(name=“CC0”, uri=”…”) >>> dataset.license = “MIT”

update_metadata(self)

Update the dataset metadata on the Dataverse server using the native API. Pushes all changes (metadata blocks and license) from this object to Dataverse.

Raises

ExceptionDescription
ValueErrorIf this dataset does not have an identifier.
publish(self, release_type: Literal['minor', 'major', 'updatecurrent'] = 'major')

Publishes a dataset within its collection.

Raises

ExceptionDescription
ValueErrorIf the dataset has no persistent identifier.
submit_to_review(self)

Submit the dataset to review.

return_to_author(self, reason: str)

Return the dataset to author.

upload_to_collection(self, collection: Union[str, Collection])

Create (upload) this dataset to a Dataverse collection using the native API.

This method sends the current state of this Dataset object as a new dataset into the target Dataverse (collection). The metadata sent includes all metadata blocks and the license as set in this object.

Parameters

NameTypeDescription
collectionstrThe dataverse alias or identifier into which this dataset will be created/uploaded.

Returns

TypeDescription
None

Raises

ExceptionDescription
httpx.HTTPStatusErrorIf the Dataverse API returns an error
ValueErrorIf required dataset metadata is missing

Example

>>> ds = Dataset(…) >>> ds.upload(“mydataverse”)

to_dataverse_create_dict(self, dataset_type: Optional[str] = None, license: Optional[str] = None) -> DatasetCreateBody

Convert this dataset instance into a Dataverse-compatible dict (for API creation/upload).

Parameters

NameTypeDescription
dataset_typeOptional[str]The dataset type tag (e.g., “dataset”). (default: None)
licenseOptional[str]License name, overrides self.license if specified. (default: None)

Returns

TypeDescription
DatasetCreateBodyPydantic model ready for Dataverse API interaction.
to_dataverse_edit_dict(self) -> edit_get.EditMetadataBody

Convert this dataset instance into a Dataverse-compatible dict (for API editing).

from_dataverse_dict(self, metadata: Union[edit_get.GetDatasetResponse, create.DatasetCreateBody]) -> Self

Update in-place this Dataset’s metadata_blocks from a Dataverse API metadata dict or model.

Parameters

NameTypeDescription
metadataedit_get.GetDatasetResponse or create.DatasetCreateBodyMetadata dict/model from Dataverse.

Returns

TypeDescription
SelfSelf, but with updated metadata blocks loaded from the input.

Raises

ExceptionDescription
AssertionErrorIf input lacks metadata blocks.

Note

Metadata blocks from Dataverse are loaded into self.metadata_blocks using each block’s own from_dataverse_dict method.

open(self, path: Union[str, File], mode: Literal['r', 'rb', 'w', 'wb'] = 'r', *, description: Optional[str] = None, categories: Optional[List[str]] = None, content_type: Optional[str] = None, tab_ingest: Optional[bool] = None) -> Union[DataverseFileReader, DataverseFileWriter]

Open a file from the dataset for reading or writing, in binary or text mode.

Parameters

NameTypeDescription
pathUnion[str, File]The file path within the dataset (e.g., “data/file.txt” or “subdir/notes.csv”) or a File object.
modeLiteral['r', 'rb', 'w', 'wb']File open mode. - “r” or “rb”: Read mode (default, “r” is text, “rb” is binary). - “w” or “wb”: Write mode (“w” is text, “wb” is binary; creates/replaces file). (default: 'r')
descriptionOptional[str]Optional file description (only available in write mode “w” or “wb”). (default: None)
categoriesOptional[List[str]]Optional list of category strings (only available in write mode “w” or “wb”). (default: None)
content_typeOptional[str]Optional MIME type (only available in write mode “w” or “wb”). (default: None)

Returns

TypeDescription
Union[DataverseFileReader, DataverseFileWriter]DataverseFileReader or DataverseFileWriter: - Reader-like object if opened in read mode. - Writer-like object if opened in write mode.

Raises

ExceptionDescription
FileNotFoundErrorIf specified file does not exist (in read mode).
ValueErrorIf mode is invalid or metadata is provided in read mode.

Examples

>>> # Read a file as text
>>> with dataset.open("data/readme.txt") as f:
... text = f.read()
>>> # Read using a File object
>>> file = dataset.files[0]
>>> with dataset.open(file) as f:
... text = f.read()
>>> # Write a new file as binary
>>> with dataset.open("outputs/results.bin", mode="wb") as f:
... f.write(b"result data")
>>> # Write with metadata
>>> with dataset.open("outputs/results.bin", mode="wb", description="My file") as f:
... f.write(b"result data")
upload_file(self, local_file_path: Union[str, Path], dataset_path: Optional[str] = None, *, description: Optional[str] = None, categories: Optional[List[str]] = None, content_type: Optional[str] = None) -> UploadResponse

Upload a local file to the dataset.

This method uploads a file from the local filesystem to the dataset. It reuses the same path and metadata handling as the open() method but uploads the file directly instead of streaming writes.

Parameters

NameTypeDescription
local_file_pathUnion[str, Path]Path to the local file to upload (string or Path object).
dataset_pathOptional[str]Optional path where the file should be stored in the dataset (e.g., “data/file.txt” or “subdir/notes.csv”). If not provided, uses the basename of the local file path. (default: None)
descriptionOptional[str]Optional file description. (default: None)
categoriesOptional[List[str]]Optional list of category strings. (default: None)
content_typeOptional[str]Optional MIME type. (default: None)
friendly_typeOptional friendly type name.
storage_identifierOptional storage identifier.
md5Optional MD5 checksum.
checksumOptional checksum dict with “type” and “value” keys.
tabular_dataOptional flag indicating if file is tabular.
file_access_requestOptional flag for file access request.
force_replaceOptional flag to force replacement if file exists.

Returns

TypeDescription
UploadResponseResponse object with upload confirmation and file information.

Raises

ExceptionDescription
FileNotFoundErrorIf the local file does not exist.
ValueErrorIf the dataset identifier is not set.

Example

>>> # Upload a local file (uses basename as dataset path) >>> dataset.upload_file(“/path/to/local/file.txt”) >>> >>> # Upload to specific dataset path >>> dataset.upload_file(“/path/to/local/file.txt”, “data/file.txt”) >>> >>> # Upload with custom metadata >>> dataset.upload_file( … “/path/to/data.csv”, … “data/data.csv”, … description=“My data file”, … categories=[“Data”, “Research”] … )

open_tabular(self, path: str, no_header: bool = False, **kwargs) -> pd.DataFrame

Load an entire tabular file (CSV, TSV, etc.) from the dataset as a pandas DataFrame.

Parameters

NameTypeDescription
pathstrPath to the file in the dataset.
sepstrDelimiter to use (default: ’,’).
no_headerboolIf True, use integer columns instead of header row. (default: False)
**kwargsAdditional keyword arguments for pandas.read_csv(). (default: {})

Returns

TypeDescription
pd.DataFramepd.DataFrame: The loaded data.

Examples

>>> df = dataset.open_tabular("data/file.csv")
>>> df = dataset.open_tabular("data/file.csv", no_header=True)
>>> df = dataset.open_tabular(
... "data/file.csv",
... usecols=["col1", "col2"],
... dtype={"col1": str, "col2": int},
... na_values=["N/A", "null"],
... )
stream_tabular(self, path: str, chunk_size: int = 10000, no_header: bool = False, sep: str = ',', **kwargs) -> Iterator[pd.DataFrame]

Stream a tabular file as multiple pandas DataFrames (chunked reads).

Parameters

NameTypeDescription
pathstrFile path in the dataset.
chunk_sizeintNumber of rows per yielded chunk (default: 10000). (default: 10000)
no_headerboolIf True, use integer columns. (default: False)
sepstrField delimiter (default: ’,’). (default: ',')
**kwargsPassed through to pandas.read_csv(). (default: {})

Examples

>>> for chunk in dataset.stream_tabular("data/file.csv"):
... print(chunk.head())
>>> for chunk in dataset.stream_tabular("data/file.csv", no_header=True):
... print(chunk.columns)
>>> for chunk in dataset.stream_tabular(
... "data/file.csv", usecols=[0, 1, 2], dtype={"col1": str}
... ):
... process(chunk)
bundle_datafiles(self, files: Union[Literal['all'], List[Union[File, str, int]]] = 'all', stream: bool = False) -> Union[bytes, _GeneratorContextManager[httpx.Response, None, None]]

Bundle one or multiple datafiles in the dataset together as a ZIP file (Dataverse “datafiles-bundle” API).

Parameters

NameTypeDescription
filesUnion[Literal['all'], List[Union[File, str, int]]]Files to include in the bundle. “all” (default) bundles all files. You can also provide a list of File objects, file paths, or file IDs. (default: 'all')
streamboolIf True, return a context manager to stream the ZIP response. (default: False)

Returns

TypeDescription
Union[bytes, _GeneratorContextManager[httpx.Response, None, None]]httpx.Response or contextlib._GeneratorContextManager[httpx.Response]: The HTTP response object containing the ZIP, or a stream context.

Raises

ExceptionDescription
ValueErrorIf any file reference is not recognized.

Examples

>>> with dataset.bundle_datafiles(files="all", stream=True) as resp:
... with open("dataset.zip", "wb") as f:
... for chunk in resp.iter_bytes():
... f.write(chunk)
>>> resp = dataset.bundle_datafiles(files=["mytab1.tab", "mytab2.tab"])
>>> with open("some.zip", "wb") as f:
... f.write(resp.read())