Skip to content

Instantly share code, notes, and snippets.

@ctuguinay
Last active April 3, 2024 17:52
Show Gist options
  • Save ctuguinay/c77461f81ee8229ea43ba2da33b7f83c to your computer and use it in GitHub Desktop.
Save ctuguinay/c77461f81ee8229ea43ba2da33b7f83c to your computer and use it in GitHub Desktop.
encode_cf_time_permutations.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from xarray import coding"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['2023-11-22T16:22:41.088137000' '2023-11-22T16:22:46.150034000'\n",
" '2023-11-22T16:22:51.140442000' '2023-11-22T16:22:56.143124000']\n"
]
}
],
"source": [
"# Create an array with a multiple datetime64 elements\n",
"datetime_array = np.array(\n",
" [\n",
" '2023-11-22T16:22:41.088137000', \n",
" '2023-11-22T16:22:46.150034000',\n",
" '2023-11-22T16:22:51.140442000', \n",
" '2023-11-22T16:22:56.143124000'\n",
" ],\n",
" dtype='datetime64[ns]'\n",
")\n",
"print(datetime_array)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Encoded: [3.90965896e+09 3.90965897e+09 3.90965897e+09 3.90965898e+09]\n",
"Units: seconds since 1900-01-01T00:00:00+00:00\n",
"Calendar: gregorian\n",
"Decoded: ['2023-11-22T16:22:41.088137216' '2023-11-22T16:22:46.150033920'\n",
" '2023-11-22T16:22:51.140442112' '2023-11-22T16:22:56.143123968']\n"
]
}
],
"source": [
"# Encode with float64\n",
"encoded, units, calendar = coding.times.encode_cf_datetime(\n",
" datetime_array,\n",
" **{\n",
" \"units\": \"seconds since 1900-01-01T00:00:00+00:00\",\n",
" \"calendar\": \"gregorian\",\n",
" \"dtype\": np.dtype(\"float64\"),\n",
" }\n",
")\n",
"print(\"Encoded:\", encoded)\n",
"print(\"Units:\", units)\n",
"print(\"Calendar:\", calendar)\n",
"decoded = coding.times.decode_cf_datetime(\n",
" encoded,\n",
" **{\n",
" \"units\": \"seconds since 1900-01-01T00:00:00+00:00\",\n",
" \"calendar\": \"gregorian\",\n",
" \n",
" }\n",
")\n",
"print(\"Decoded:\", decoded)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Encoded: [3909658961088137 3909658966150034 3909658971140442 3909658976143124]\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_2437/2257729061.py:2: UserWarning: Times can't be serialized faithfully to int64 with requested units 'seconds since 1900-01-01T00:00:00+00:00'. Serializing with units 'microseconds since 1900-01-01' instead. Set encoding['dtype'] to floating point dtype to serialize with units 'seconds since 1900-01-01T00:00:00+00:00'. Set encoding['units'] to 'microseconds since 1900-01-01' to silence this warning .\n",
" encoded, _, _ = coding.times.encode_cf_datetime(\n"
]
},
{
"ename": "OverflowError",
"evalue": "time values outside range of 64 bit signed integers",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32mconversion.pyx:228\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.conversion.cast_from_unit\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOverflowError\u001b[0m: Python int too large to convert to C long",
"\nThe above exception was the direct cause of the following exception:\n",
"\u001b[0;31mOutOfBoundsDatetime\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32mtimedeltas.pyx:377\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.timedeltas._maybe_cast_from_unit\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32mconversion.pyx:230\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.conversion.cast_from_unit\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOutOfBoundsDatetime\u001b[0m: cannot convert input 3909658961088137 with the unit 's'",
"\nThe above exception was the direct cause of the following exception:\n",
"\u001b[0;31mOutOfBoundsTimedelta\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/xarray/coding/times.py:322\u001b[0m, in \u001b[0;36mdecode_cf_datetime\u001b[0;34m(num_dates, units, calendar, use_cftime)\u001b[0m\n\u001b[1;32m 321\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 322\u001b[0m dates \u001b[38;5;241m=\u001b[39m \u001b[43m_decode_datetime_with_pandas\u001b[49m\u001b[43m(\u001b[49m\u001b[43mflat_num_dates\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 323\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mKeyError\u001b[39;00m, OutOfBoundsDatetime, OutOfBoundsTimedelta, \u001b[38;5;167;01mOverflowError\u001b[39;00m):\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/xarray/coding/times.py:270\u001b[0m, in \u001b[0;36m_decode_datetime_with_pandas\u001b[0;34m(flat_num_dates, units, calendar)\u001b[0m\n\u001b[1;32m 268\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m flat_num_dates\u001b[38;5;241m.\u001b[39msize \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 269\u001b[0m \u001b[38;5;66;03m# avoid size 0 datetimes GH1329\u001b[39;00m\n\u001b[0;32m--> 270\u001b[0m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto_timedelta\u001b[49m\u001b[43m(\u001b[49m\u001b[43mflat_num_dates\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmin\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtime_units\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m+\u001b[39m ref_date\n\u001b[1;32m 271\u001b[0m pd\u001b[38;5;241m.\u001b[39mto_timedelta(flat_num_dates\u001b[38;5;241m.\u001b[39mmax(), time_units) \u001b[38;5;241m+\u001b[39m ref_date\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/pandas/core/tools/timedeltas.py:223\u001b[0m, in \u001b[0;36mto_timedelta\u001b[0;34m(arg, unit, errors)\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[38;5;66;03m# ...so it must be a scalar value. Return scalar.\u001b[39;00m\n\u001b[0;32m--> 223\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_coerce_scalar_to_timedelta_type\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munit\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43munit\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43merrors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43merrors\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/pandas/core/tools/timedeltas.py:233\u001b[0m, in \u001b[0;36m_coerce_scalar_to_timedelta_type\u001b[0;34m(r, unit, errors)\u001b[0m\n\u001b[1;32m 232\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 233\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mTimedelta\u001b[49m\u001b[43m(\u001b[49m\u001b[43mr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munit\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 234\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m:\n",
"File \u001b[0;32mtimedeltas.pyx:1896\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.timedeltas.Timedelta.__new__\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32mtimedeltas.pyx:354\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.timedeltas.convert_to_timedelta64\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32mtimedeltas.pyx:379\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.timedeltas._maybe_cast_from_unit\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOutOfBoundsTimedelta\u001b[0m: Cannot cast 3909658961088137 from s to 'ns' without overflow.",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[8], line 11\u001b[0m\n\u001b[1;32m 2\u001b[0m encoded, _, _ \u001b[38;5;241m=\u001b[39m coding\u001b[38;5;241m.\u001b[39mtimes\u001b[38;5;241m.\u001b[39mencode_cf_datetime(\n\u001b[1;32m 3\u001b[0m datetime_array,\n\u001b[1;32m 4\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m{\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 8\u001b[0m }\n\u001b[1;32m 9\u001b[0m )\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEncoded:\u001b[39m\u001b[38;5;124m\"\u001b[39m, encoded)\n\u001b[0;32m---> 11\u001b[0m decoded \u001b[38;5;241m=\u001b[39m \u001b[43mcoding\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtimes\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode_cf_datetime\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 12\u001b[0m \u001b[43m \u001b[49m\u001b[43mencoded\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 13\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m{\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43munits\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mseconds since 1900-01-01T00:00:00+00:00\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 15\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcalendar\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgregorian\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 19\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDecoded:\u001b[39m\u001b[38;5;124m\"\u001b[39m, decoded)\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/xarray/coding/times.py:324\u001b[0m, in \u001b[0;36mdecode_cf_datetime\u001b[0;34m(num_dates, units, calendar, use_cftime)\u001b[0m\n\u001b[1;32m 322\u001b[0m dates \u001b[38;5;241m=\u001b[39m _decode_datetime_with_pandas(flat_num_dates, units, calendar)\n\u001b[1;32m 323\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mKeyError\u001b[39;00m, OutOfBoundsDatetime, OutOfBoundsTimedelta, \u001b[38;5;167;01mOverflowError\u001b[39;00m):\n\u001b[0;32m--> 324\u001b[0m dates \u001b[38;5;241m=\u001b[39m \u001b[43m_decode_datetime_with_cftime\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 325\u001b[0m \u001b[43m \u001b[49m\u001b[43mflat_num_dates\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mastype\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\n\u001b[1;32m 326\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 328\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m 329\u001b[0m dates[np\u001b[38;5;241m.\u001b[39mnanargmin(num_dates)]\u001b[38;5;241m.\u001b[39myear \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m1678\u001b[39m\n\u001b[1;32m 330\u001b[0m \u001b[38;5;129;01mor\u001b[39;00m dates[np\u001b[38;5;241m.\u001b[39mnanargmax(num_dates)]\u001b[38;5;241m.\u001b[39myear \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2262\u001b[39m\n\u001b[1;32m 331\u001b[0m ):\n\u001b[1;32m 332\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _is_standard_calendar(calendar):\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/xarray/coding/times.py:240\u001b[0m, in \u001b[0;36m_decode_datetime_with_cftime\u001b[0;34m(num_dates, units, calendar)\u001b[0m\n\u001b[1;32m 237\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mModuleNotFoundError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo module named \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcftime\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 238\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m num_dates\u001b[38;5;241m.\u001b[39msize \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 239\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39masarray(\n\u001b[0;32m--> 240\u001b[0m \u001b[43mcftime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnum2date\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnum_dates\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43monly_use_cftime_datetimes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 241\u001b[0m )\n\u001b[1;32m 242\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 243\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39marray([], dtype\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mobject\u001b[39m)\n",
"File \u001b[0;32msrc/cftime/_cftime.pyx:617\u001b[0m, in \u001b[0;36mcftime._cftime.num2date\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32msrc/cftime/_cftime.pyx:414\u001b[0m, in \u001b[0;36mcftime._cftime.cast_to_int\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOverflowError\u001b[0m: time values outside range of 64 bit signed integers"
]
}
],
"source": [
"# Encode with int64\n",
"encoded, _, _ = coding.times.encode_cf_datetime(\n",
" datetime_array,\n",
" **{\n",
" \"units\": \"seconds since 1900-01-01T00:00:00+00:00\",\n",
" \"calendar\": \"gregorian\",\n",
" \"dtype\": np.dtype(\"int64\"),\n",
" }\n",
")\n",
"print(\"Encoded:\", encoded)\n",
"decoded = coding.times.decode_cf_datetime(\n",
" encoded,\n",
" **{\n",
" \"units\": \"seconds since 1900-01-01T00:00:00+00:00\",\n",
" \"calendar\": \"gregorian\",\n",
" \n",
" }\n",
")\n",
"print(\"Decoded:\", decoded)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Encoded: [3909658961088137 3909658966150034 3909658971140442 3909658976143124]\n"
]
},
{
"ename": "OverflowError",
"evalue": "time values outside range of 64 bit signed integers",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32mconversion.pyx:228\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.conversion.cast_from_unit\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOverflowError\u001b[0m: Python int too large to convert to C long",
"\nThe above exception was the direct cause of the following exception:\n",
"\u001b[0;31mOutOfBoundsDatetime\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32mtimedeltas.pyx:377\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.timedeltas._maybe_cast_from_unit\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32mconversion.pyx:230\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.conversion.cast_from_unit\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOutOfBoundsDatetime\u001b[0m: cannot convert input 3909658961088137 with the unit 's'",
"\nThe above exception was the direct cause of the following exception:\n",
"\u001b[0;31mOutOfBoundsTimedelta\u001b[0m Traceback (most recent call last)",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/xarray/coding/times.py:322\u001b[0m, in \u001b[0;36mdecode_cf_datetime\u001b[0;34m(num_dates, units, calendar, use_cftime)\u001b[0m\n\u001b[1;32m 321\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 322\u001b[0m dates \u001b[38;5;241m=\u001b[39m \u001b[43m_decode_datetime_with_pandas\u001b[49m\u001b[43m(\u001b[49m\u001b[43mflat_num_dates\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 323\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mKeyError\u001b[39;00m, OutOfBoundsDatetime, OutOfBoundsTimedelta, \u001b[38;5;167;01mOverflowError\u001b[39;00m):\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/xarray/coding/times.py:270\u001b[0m, in \u001b[0;36m_decode_datetime_with_pandas\u001b[0;34m(flat_num_dates, units, calendar)\u001b[0m\n\u001b[1;32m 268\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m flat_num_dates\u001b[38;5;241m.\u001b[39msize \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 269\u001b[0m \u001b[38;5;66;03m# avoid size 0 datetimes GH1329\u001b[39;00m\n\u001b[0;32m--> 270\u001b[0m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto_timedelta\u001b[49m\u001b[43m(\u001b[49m\u001b[43mflat_num_dates\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmin\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtime_units\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m+\u001b[39m ref_date\n\u001b[1;32m 271\u001b[0m pd\u001b[38;5;241m.\u001b[39mto_timedelta(flat_num_dates\u001b[38;5;241m.\u001b[39mmax(), time_units) \u001b[38;5;241m+\u001b[39m ref_date\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/pandas/core/tools/timedeltas.py:223\u001b[0m, in \u001b[0;36mto_timedelta\u001b[0;34m(arg, unit, errors)\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[38;5;66;03m# ...so it must be a scalar value. Return scalar.\u001b[39;00m\n\u001b[0;32m--> 223\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_coerce_scalar_to_timedelta_type\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munit\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43munit\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43merrors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43merrors\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/pandas/core/tools/timedeltas.py:233\u001b[0m, in \u001b[0;36m_coerce_scalar_to_timedelta_type\u001b[0;34m(r, unit, errors)\u001b[0m\n\u001b[1;32m 232\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 233\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mTimedelta\u001b[49m\u001b[43m(\u001b[49m\u001b[43mr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munit\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 234\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m:\n",
"File \u001b[0;32mtimedeltas.pyx:1896\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.timedeltas.Timedelta.__new__\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32mtimedeltas.pyx:354\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.timedeltas.convert_to_timedelta64\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32mtimedeltas.pyx:379\u001b[0m, in \u001b[0;36mpandas._libs.tslibs.timedeltas._maybe_cast_from_unit\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOutOfBoundsTimedelta\u001b[0m: Cannot cast 3909658961088137 from s to 'ns' without overflow.",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mOverflowError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[10], line 11\u001b[0m\n\u001b[1;32m 2\u001b[0m encoded, _, _ \u001b[38;5;241m=\u001b[39m coding\u001b[38;5;241m.\u001b[39mtimes\u001b[38;5;241m.\u001b[39mencode_cf_datetime(\n\u001b[1;32m 3\u001b[0m datetime_array,\n\u001b[1;32m 4\u001b[0m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m{\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 8\u001b[0m }\n\u001b[1;32m 9\u001b[0m )\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEncoded:\u001b[39m\u001b[38;5;124m\"\u001b[39m, encoded)\n\u001b[0;32m---> 11\u001b[0m decoded \u001b[38;5;241m=\u001b[39m \u001b[43mcoding\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtimes\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode_cf_datetime\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 12\u001b[0m \u001b[43m \u001b[49m\u001b[43mencoded\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 13\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m{\u001b[49m\n\u001b[1;32m 14\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43munits\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mseconds since 1900-01-01T00:00:00+00:00\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 15\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcalendar\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mgregorian\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 16\u001b[0m \u001b[43m \u001b[49m\n\u001b[1;32m 17\u001b[0m \u001b[43m \u001b[49m\u001b[43m}\u001b[49m\n\u001b[1;32m 18\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m 19\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDecoded:\u001b[39m\u001b[38;5;124m\"\u001b[39m, decoded)\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/xarray/coding/times.py:324\u001b[0m, in \u001b[0;36mdecode_cf_datetime\u001b[0;34m(num_dates, units, calendar, use_cftime)\u001b[0m\n\u001b[1;32m 322\u001b[0m dates \u001b[38;5;241m=\u001b[39m _decode_datetime_with_pandas(flat_num_dates, units, calendar)\n\u001b[1;32m 323\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mKeyError\u001b[39;00m, OutOfBoundsDatetime, OutOfBoundsTimedelta, \u001b[38;5;167;01mOverflowError\u001b[39;00m):\n\u001b[0;32m--> 324\u001b[0m dates \u001b[38;5;241m=\u001b[39m \u001b[43m_decode_datetime_with_cftime\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 325\u001b[0m \u001b[43m \u001b[49m\u001b[43mflat_num_dates\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mastype\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\n\u001b[1;32m 326\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 328\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m 329\u001b[0m dates[np\u001b[38;5;241m.\u001b[39mnanargmin(num_dates)]\u001b[38;5;241m.\u001b[39myear \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m1678\u001b[39m\n\u001b[1;32m 330\u001b[0m \u001b[38;5;129;01mor\u001b[39;00m dates[np\u001b[38;5;241m.\u001b[39mnanargmax(num_dates)]\u001b[38;5;241m.\u001b[39myear \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2262\u001b[39m\n\u001b[1;32m 331\u001b[0m ):\n\u001b[1;32m 332\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _is_standard_calendar(calendar):\n",
"File \u001b[0;32m~/miniforge3/envs/echopype/lib/python3.9/site-packages/xarray/coding/times.py:240\u001b[0m, in \u001b[0;36m_decode_datetime_with_cftime\u001b[0;34m(num_dates, units, calendar)\u001b[0m\n\u001b[1;32m 237\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mModuleNotFoundError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo module named \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcftime\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 238\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m num_dates\u001b[38;5;241m.\u001b[39msize \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 239\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39masarray(\n\u001b[0;32m--> 240\u001b[0m \u001b[43mcftime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnum2date\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnum_dates\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43munits\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcalendar\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43monly_use_cftime_datetimes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 241\u001b[0m )\n\u001b[1;32m 242\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 243\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39marray([], dtype\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mobject\u001b[39m)\n",
"File \u001b[0;32msrc/cftime/_cftime.pyx:617\u001b[0m, in \u001b[0;36mcftime._cftime.num2date\u001b[0;34m()\u001b[0m\n",
"File \u001b[0;32msrc/cftime/_cftime.pyx:414\u001b[0m, in \u001b[0;36mcftime._cftime.cast_to_int\u001b[0;34m()\u001b[0m\n",
"\u001b[0;31mOverflowError\u001b[0m: time values outside range of 64 bit signed integers"
]
}
],
"source": [
"# Encode with int64 and microseconds since ...\n",
"encoded, _, _ = coding.times.encode_cf_datetime(\n",
" datetime_array,\n",
" **{\n",
" \"units\": \"microseconds since 1900-01-01T00:00:00+00:00\",\n",
" \"calendar\": \"gregorian\",\n",
" \"dtype\": np.dtype(\"int64\"),\n",
" }\n",
")\n",
"print(\"Encoded:\", encoded)\n",
"decoded = coding.times.decode_cf_datetime(\n",
" encoded,\n",
" **{\n",
" \"units\": \"seconds since 1900-01-01T00:00:00+00:00\",\n",
" \"calendar\": \"gregorian\",\n",
" \n",
" }\n",
")\n",
"print(\"Decoded:\", decoded)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Encoded: [3.90965896e+09 3.90965897e+09 3.90965897e+09 3.90965898e+09]\n",
"Decoded: ['2023-11-22T16:22:41.088137216' '2023-11-22T16:22:46.150033920'\n",
" '2023-11-22T16:22:51.140442112' '2023-11-22T16:22:56.143123968']\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_2437/1501405369.py:2: UserWarning: Times can't be serialized faithfully to int64 with requested units 'seconds since 1900-01-01T00:00:00+00:00'. Resolution of 'microseconds' needed. Serializing times to floating point instead. Set encoding['dtype'] to integer dtype to serialize to int64. Set encoding['dtype'] to floating point dtype to silence this warning.\n",
" encoded, _, _ = coding.times.encode_cf_datetime(\n"
]
}
],
"source": [
"# Encode without dtype\n",
"encoded, _, _ = coding.times.encode_cf_datetime(\n",
" datetime_array,\n",
" **{\n",
" \"units\": \"seconds since 1900-01-01T00:00:00+00:00\",\n",
" \"calendar\": \"gregorian\",\n",
" }\n",
")\n",
"print(\"Encoded:\", encoded)\n",
"decoded = coding.times.decode_cf_datetime(\n",
" encoded,\n",
" **{\n",
" \"units\": \"seconds since 1900-01-01T00:00:00+00:00\",\n",
" \"calendar\": \"gregorian\",\n",
" \n",
" }\n",
")\n",
"print(\"Decoded:\", decoded)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "echopype",
"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.18"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment