Как в pandas создать серию, у которой в индексе время суток, а в значениях - числа?
- User
Как в pandas создать серию, у которой в индексе время суток, а в значениях - числа?
Assistant
Вот несколько способов создать Series с временем суток в индексе:
import pandas as pd import numpy as np # Способ 1: Используя pd.date_range() для создания временных меток time_index = pd.date_range('2024-01-01', periods=24, freq='h') values = np.random.randint(0, 100, 24) series1 = pd.Series(values, index=time_index) # Способ 2: Создание времени без даты (только время суток) time_index = pd.date_range('00:00', '23:00', freq='h').time values = np.random.randint(0, 100, 24) series2 = pd.Series(values, index=time_index) # Способ 3: Используя pd.timedelta_range() для времени от начала суток time_index = pd.timedelta_range(start='0 hours', end='23 hours', freq='h') values = np.random.randint(0, 100, 24) series3 = pd.Series(values, index=time_index) # Способ 4: Создание произвольных временных точек times = ['08:30', '12:00', '15:45', '18:20', '22:10'] time_index = pd.to_datetime(times, format='%H:%M').time values = [25, 50, 75, 60, 30] series4 = pd.Series(values, index=time_index)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.