6 "id": "5db541f3-072f-4ac4-88fe-9dd744188c26",
11 "output_type": "stream",
13 "t2 correctly increments by one hour.\n",
14 "DatetimeIndex(['2023-01-01 12:00:00', '2023-01-01 13:00:00',\n",
15 " '2023-01-01 14:00:00', '2023-01-01 15:00:00'],\n",
16 " dtype='datetime64[ns]', freq='H')\n",
17 "[10. 16.66666667 18.33333333 15. ]\n"
22 "import numpy as np\n",
23 "import pandas as pd\n",
25 "# Sample data: Replace these with your actual datetime arrays and value arrays\n",
26 "t1 = pd.to_datetime(['2023-01-01 12:00', '2023-01-01 13:30', '2023-01-01 15:00'])\n",
27 "v1 = np.array([10, 20, 15])\n",
29 "t2 = pd.date_range(start='2023-01-01 12:00', periods=4, freq='H')\n",
31 "# Create pandas Series from t1 and v1\n",
32 "series_v1 = pd.Series(data=v1, index=t1)\n",
34 "# Check that t2 increments by one hour\n",
35 "if all(t2 == pd.date_range(start=t2[0], periods=len(t2), freq='H')):\n",
36 " print(\"t2 correctly increments by one hour.\")\n",
38 " print(\"Error: t2 does not increment correctly by one hour.\")\n",
40 "# Interpolate (linearly) and extrapolate at endpoints\n",
41 "series_v3 = series_v1.reindex(series_v1.index.union(t2)).interpolate(method='time').reindex(t2)\n",
43 "# Check for need to extrapolate by constant at the endpoints\n",
44 "if t2[0] < series_v1.index[0]:\n",
45 " series_v3[t2[0]] = series_v1.iloc[0]\n",
46 "if t2[-1] > series_v1.index[-1]:\n",
47 " series_v3[t2[-1]] = series_v1.iloc[-1]\n",
49 "# Prepare final output\n",
50 "t3 = series_v3.index\n",
51 "v3 = series_v3.values\n",
60 "id": "5c6c16b5-4c90-4b61-adce-441a6282146e",
64 "import numpy as np\n",
65 "import pandas as pd\n",
66 "from utils import time_intp\n"
72 "id": "09768b38-ef92-4c8d-a0ed-31fb347f3883",
77 "output_type": "stream",
79 "t2 times: [Timestamp('2023-01-01 00:00:00') Timestamp('2023-01-03 00:00:00')\n",
80 " Timestamp('2023-01-07 00:00:00') Timestamp('2023-01-10 00:00:00')]\n",
81 "Interpolated v2 values: [1. 1.44444444 2.33333333 3. ]\n"
86 "def time_intp_test():\n",
87 " # Create test data\n",
88 " t1 = np.array([pd.Timestamp('2023-01-01'), pd.Timestamp('2023-01-05'), pd.Timestamp('2023-01-10')])\n",
89 " v1 = np.array([1, np.nan, 3])\n",
90 " t2 = np.array([pd.Timestamp('2023-01-01'), pd.Timestamp('2023-01-03'), pd.Timestamp('2023-01-07'), pd.Timestamp('2023-01-10')])\n",
92 " # Test the interpolation function\n",
93 " v2 = time_intp(t1, v1, t2)\n",
95 " # Output the results for inspection\n",
96 " print(\"t2 times:\", t2)\n",
97 " print(\"Interpolated v2 values:\", v2)\n",
99 "# Run the test function to see results\n",
105 "execution_count": null,
106 "id": "2ebbbc3b-0080-4ed1-b60a-41d374f4247d",
114 "display_name": "Python 3 (ipykernel)",
115 "language": "python",
123 "file_extension": ".py",
124 "mimetype": "text/x-python",
126 "nbconvert_exporter": "python",
127 "pygments_lexer": "ipython3",