$date in collections (Python)

The handling of datetime objects changed in Python client version 2.0.

DataStax recommends upgrading to the latest version of the Python client. For more information, see Data API client upgrade guide (Python).

The Python client provides the DataAPITimestamp and DataAPIDate objects for storing dates. However, if you insert a document with a DataAPIDate value, the client will always return DataAPITimestamp. DataStax recommends that you use DataAPITimestamp instead of DataAPIDate for collections since equality filters on DataAPIDate may not behave as expected if the implicit conversion to DataAPITimestamp uses a different timezone than expected. The DataAPITime and DataAPIDuration objects cannot be used with collections. For more information, see Python client usage: Client custom data types.

The Python client also supports standard-library date and datetime. If you use a standard-library datetime that is not timezone-aware (or just a date, which is always timezone-naive), the client will raise an error by default. For more information, see Python client usage: DataAPITimestamp and datetimes.

from datetime import datetime

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.data_types import DataAPITimestamp

# Get an existing collection
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
    keyspace="KEYSPACE_NAME",
)
collection = database.get_collection("COLLECTION_NAME")

# Use DataAPITimestamp in insertions
collection.insert_one(
    {"when": DataAPITimestamp.from_string("2024-12-06T12:34:56.789Z")}
)
collection.insert_one(
    {"registered_at": DataAPITimestamp.from_datetime(datetime.now())}
)

# Use $current date in an update
collection.update_one(
    {"registered_at": {"$exists": True}},
    {"$currentDate": {"last_reviewed": True}},
)

# Use DataAPITimestamp in an equality filter
collection.update_one(
    {
        "registered_at": DataAPITimestamp.from_string(
            "2024-12-06T12:34:56.789Z"
        )
    },
    {"$set": {"message": "happy Sunday!"}},
)

# Use DataAPITimestamp in a "less than" filter
collection.find_one(
    {
        "registered_at": {
            "$lt": DataAPITimestamp.from_string(
                "2025-12-06T12:34:56.789Z"
            )
        }
    }
)

# Print a document with a date
# Will print something like:
# {'registered_at': DataAPITimestamp(timestamp_ms=1733488496789 [2024-12-06T12:34:56.789Z])}
print(
    collection.find_one(
        {
            "registered_at": {
                "$lt": DataAPITimestamp.from_string(
                    "2025-12-06T12:34:56.789Z"
                )
            }
        },
        projection={"_id": False},
    )
)

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM