tVoltageCurve = [
0.009585,
0.009593,
0.020547,
0.020555,
0.031541,
0.031564,
0.042611,
0.042641,
0.053560,
0.053586,
0.067775,
0.067797,
0.078824,
0.078832,
0.089813,
0.089821,
]
voltageCurve = [
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
63.0,
]
time_series_data = []
# Add the time series
name = "Total Voltage (V)"
timestamps = tVoltageCurve
values = voltageCurve
if len(timestamps)> 0:
df = pd.DataFrame({'timestamp': timestamps, 'value': values})
print(f"{name}: df columns: {df.columns} and df is {df}")
# Set the timestamp as the index for easier time-based operations
df.set_index('timestamp', inplace=True)
time_series_data.append((df,name))
synchronize_and_export_time_series(time_series_data, 'synchronized_time_series.csv')
else:
print(f"
def synchronize_and_export_time_series(time_series_data, output_file):
"""
Synchronizes multiple time series and exports them to a CSV file.
Args:
time_series_data: A list of tuples, where each tuple contains:
- A DataFrame representing a time series.
- A string representing the series name.
output_file: The path to the output CSV file.
"""
# Create a DataFrame for each time series
dataframes = []
for df, name in time_series_data:
print(f"synchronize_and_export_time_series: df:{df} | name:{name}")
df.set_index('timestamp', inplace=True) # Set timestamp as index
dataframes.append(df.rename(columns={'value': name}))
# Merge DataFrames, aligning on the index (timestamp)
merged_df = pd.concat(dataframes, axis=1)
# Resample to a common frequency (adjust as needed)
resampled_df = merged_df.resample('1S').mean() # Resample to 1-second intervals
# Interpolate missing values (optional)
interpolated_df = resampled_df.interpolate(method='linear')
# Export to CSV
interpolated_df.to_csv(output_file)
Но в моих данных есть столбец «метка времени», как показано в журналах ниже:
Total Voltage (V): df columns: Index(['timestamp', 'value'], dtype='object') and df is timestamp value
0 0.009585 63.0
1 0.009593 63.0
2 0.020547 63.0
3 0.020555 63.0
4 0.031541 63.0
5 0.031564 63.0
6 0.042611 63.0
7 0.042641 63.0
8 0.053560 63.0
9 0.053586 63.0
10 0.067775 63.0
11 0.067797 63.0
12 0.078824 63.0
13 0.078832 63.0
14 0.089813 63.0
15 0.089821 63.0
synchronize_and_export_time_series: df: value
timestamp
0.009585 63.0
0.009593 63.0
0.020547 63.0
0.020555 63.0
0.031541 63.0
0.031564 63.0
0.042611 63.0
0.042641 63.0
0.053560 63.0
0.053586 63.0
0.067775 63.0
0.067797 63.0
0.078824 63.0
0.078832 63.0
0.089813 63.0
0.089821 63.0 | name:Total Voltage (V)
Traceback (most recent call last):
File "/Users/stephanedeluca/Documents/pepsr-excalibur.py", line 2482, in
synchronize_and_export_time_series(time_series_data, 'synchronized_time_series.csv')
File "/Users/stephanedeluca/Documents/pepsr-excalibur/pepsr-excalibur.py", line 1194, in synchronize_and_export_time_series
df.set_index('timestamp', inplace=True) # Set timestamp as index
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/stephanedeluca/Library/Python/3.12/lib/python/site-packages/pandas/core/frame.py", line 6122, in set_index
raise KeyError(f"None of {missing} are in the columns")
KeyError: "None of ['timestamp'] are in the columns"
Подробнее здесь: https://stackoverflow.com/questions/791 ... ith-pandas
Мобильная версия