[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-02 (世界標準時間)。"],[],[],null,["# Serve historical feature values\n\n| **Preview**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nIf you need to retrieve or serve current as well as historical\nfeature data, use offline serving to fetch feature values from\nBigQuery. For example, you can use offline serving to retrieve\nthe feature values for specific timestamps to train a model.\n\nAll feature data, including historical feature data, is maintained in\nBigQuery, which constitutes the offline store for your feature values.\nTo use offline serving, you must first register your BigQuery\ndata source by creating feature groups and feature values. Also, in the case of\noffline serving, every row containing the same entity ID must have a\ndifferent timestamp. For more information about data source\npreparation guidelines, see [Prepare data source](/vertex-ai/docs/featurestore/latest/prepare-data-source).\n\nBefore you begin\n----------------\n\n\nto\nVertex AI, unless you've done so already.\n\n\nTo use the Python samples on this page in a local\ndevelopment environment, install and initialize the gcloud CLI, and\nthen set up Application Default Credentials with your user credentials.\n\n1. [Install](/sdk/docs/install) the Google Cloud CLI.\n2. If you're using an external identity provider (IdP), you must first [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n3. If you're using a local shell, then create local authentication credentials for your user account: \n\n```bash\ngcloud auth application-default login\n```\n4. You don't need to do this if you're using Cloud Shell.\n5. If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have [signed in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\nFor more information, see\n[Set up authentication for a local development environment](/vertex-ai/docs/authentication#local-development).\n\nFetch historical feature values\n-------------------------------\n\nUse the following sample to fetch historical values from a feature from multiple\nentity IDs and timestamps. \n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Vertex AI quickstart using\nclient libraries](/vertex-ai/docs/start/client-libraries).\n\n\nFor more information, see the\n[Vertex AI Python API\nreference documentation](/python/docs/reference/aiplatform/latest).\n\n\nTo authenticate to Vertex AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import https://cloud.google.com/python/docs/reference/bigframes/latest/\n import bigframes.pandas\n import pandas as pd\n from google.cloud import https://cloud.google.com/python/docs/reference/bigquery/latest/\n from vertexai.resources.preview.feature_store import (Feature, FeatureGroup, offline_store)\n from vertexai.resources.preview.feature_store import utils as fs_utils\n\n fg = FeatureGroup(\"\u003cvar translate=\"no\"\u003eFEATURE_GROUP_NAME\u003c/var\u003e\")\n f1 = fg.get_feature(\"\u003cvar translate=\"no\"\u003eFEATURE_NAME_1\u003c/var\u003e\")\n f2 = fg.get_feature(\"\u003cvar translate=\"no\"\u003eFEATURE_NAME_2\u003c/var\u003e\")\n\n entity_df = pd.DataFrame(\n data={\n \"\u003cvar translate=\"no\"\u003eENTITY_ID_COLUMN\u003c/var\u003e\": [\n \"\u003cvar translate=\"no\"\u003eENTITY_ID_1\u003c/var\u003e\",\n \"\u003cvar translate=\"no\"\u003eENTITY_ID_2\u003c/var\u003e\",\n ],\n \"timestamp\": [\n pd.Timestamp(\"\u003cvar translate=\"no\"\u003eFEATURE_TIMESTAMP_1\u003c/var\u003e\"),\n pd.Timestamp(\"\u003cvar translate=\"no\"\u003eFEATURE_TIMESTAMP_2\u003c/var\u003e\"),\n ],\n },\n )\n\n offline_store.fetch_historical_feature_values(\n entity_df=entity_df,\n features=[f1,f2],\n )\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eFEATURE_GROUP_NAME\u003c/var\u003e: The name of the existing feature group\n containing the feature.\n\n- \u003cvar translate=\"no\"\u003eFEATURE_NAME_1\u003c/var\u003e and \u003cvar translate=\"no\"\u003eFEATURE_NAME_2\u003c/var\u003e: The names of the\n registered features from which you want to retrieve the feature values.\n\n- \u003cvar translate=\"no\"\u003eENTITY_ID_COLUMN\u003c/var\u003e: The name of the column containing the entity IDs.\n You can specify a column name only if it's registered in the feature group.\n\n- \u003cvar translate=\"no\"\u003eENTITY_ID_1\u003c/var\u003e and \u003cvar translate=\"no\"\u003eENTITY_ID_2\u003c/var\u003e: The entity IDs for which\n you want to fetch the feature values. If you want to retrieve feature values\n for the same entity ID at different timestamps, specify the same entity ID\n corresponding to each timestamp.\n\n- \u003cvar translate=\"no\"\u003eFEATURE_TIMESTAMP_1\u003c/var\u003e and \u003cvar translate=\"no\"\u003eFEATURE_TIMESTAMP_2\u003c/var\u003e: The\n timestamps corresponding to the historical feature values you want to retrieve.\n \u003cvar translate=\"no\"\u003eFEATURE_TIMESTAMP_1\u003c/var\u003e corresponds to \u003cvar translate=\"no\"\u003eENTITY_ID_1\u003c/var\u003e,\n \u003cvar translate=\"no\"\u003eFEATURE_TIMESTAMP_2\u003c/var\u003e corresponds to \u003cvar translate=\"no\"\u003eENTITY_ID_2\u003c/var\u003e, and so on.\n Specify the timestamps in the datetime format---for example, `2024-05-01T12:00:00`.\n\n\n\u003cbr /\u003e"]]