{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 生長度日\n", "\n", "此筆記本載入保存在 CSV 文件中的溫度數據,並進行分析。它會繪製溫度圖表,顯示每天的最高和最低溫度,並計算 GDD。\n", "\n", "使用此筆記本的方法:\n", "\n", "* 將 `temperature.csv` 文件複製到與此筆記本相同的文件夾中\n", "* 使用上方的 **▶︎ Run** 按鈕運行所有單元格。這將運行選定的單元格,然後移至下一個單元格。\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "在下面的單元格中,將 `base_temperature` 設置為植物的基礎溫度。\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "base_temperature = 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "現在需要使用 pandas 加載 CSV 文件\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "\n", "# Read the temperature CSV file\n", "df = pd.read_csv('temperature.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(20, 10))\n", "plt.plot(df['date'], df['temperature'])\n", "plt.xticks(rotation='vertical');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "一旦讀取了數據,就可以按 `date` 列進行分組,並提取每個日期的最低和最高溫度。\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Convert datetimes to pure dates so we can group by the date\n", "df['date'] = pd.to_datetime(df['date']).dt.date\n", "\n", "# Group the data by date so it can be analyzed by date\n", "data_by_date = df.groupby('date')\n", "\n", "# Get the minimum and maximum temperatures for each date\n", "min_by_date = data_by_date.min()\n", "max_by_date = data_by_date.max()\n", "\n", "# Join the min and max temperatures into one dataframe and flatten it\n", "min_max_by_date = min_by_date.join(max_by_date, on='date', lsuffix='_min', rsuffix='_max')\n", "min_max_by_date = min_max_by_date.reset_index()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可以使用標準GDD方程式計算GDD\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def calculate_gdd(row):\n", " return ((row['temperature_max'] + row['temperature_min']) / 2) - base_temperature\n", "\n", "# Calculate the GDD for each row\n", "min_max_by_date['gdd'] = min_max_by_date.apply (lambda row: calculate_gdd(row), axis=1)\n", "\n", "# Print the results\n", "print(min_max_by_date[['date', 'gdd']].to_string(index=False))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n---\n\n**免責聲明**: \n本文件已使用人工智能翻譯服務 [Co-op Translator](https://github.com/Azure/co-op-translator) 進行翻譯。儘管我們致力於提供準確的翻譯,但請注意,自動翻譯可能包含錯誤或不準確之處。原始語言的文件應被視為權威來源。對於重要資訊,建議使用專業人工翻譯。我們對因使用此翻譯而引起的任何誤解或錯誤解釋概不負責。\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.1" }, "metadata": { "interpreter": { "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" } }, "coopTranslator": { "original_hash": "8fcf954f6042f0bf3601a2c836a09574", "translation_date": "2025-08-26T15:58:15+00:00", "source_file": "2-farm/lessons/1-predict-plant-growth/code-notebook/gdd.ipynb", "language_code": "hk" } }, "nbformat": 4, "nbformat_minor": 2 }