Я пытаюсь развернуть код Python в Google, чтобы получить результаты в VBA. Код хорошо работает в PyCharm, но возвращает ошибку 500 от Google.
Требования
scipy
flask==2.0.3
werkzeug==2.0.3
pandas
numpy
statsmodels
google-cloud-storage
Python
from statsmodels.stats.multicomp import pairwise_tukeyhsd
from google.cloud import storage
def tukey_hsd(request):
# Initialize the storage client
storage_client = storage.Client()
bucket_name = "your-bucket-name" # Replace with your GCS bucket name
bucket = storage_client.bucket(bucket_name)
# Load the CSV file into a DataFrame
# For cloud function testing, you can simulate data instead of loading a CSV file from local disk
data = pd.DataFrame({
0: [5, 5.5, 6, 7, np.nan],
1: [6, 7, 8, np.nan, 9],
2: [7, 8, 9, 8.5, np.nan]
})
# Prepare values and groups arrays
values = []
groups = []
group_names = ['Group1', 'Group2', 'Group3']
for i, group_name in enumerate(group_names):
# Convert all values in the column to numeric, setting non-numeric values to NaN
numeric_col = pd.to_numeric(data, errors='coerce')
# Drop missing (NaN) values
col_values = numeric_col.dropna().values
# Add to values list and assign group labels
values.extend(col_values)
groups.extend([group_name] * len(col_values))
# Convert lists to NumPy arrays
values = np.array(values)
groups = np.array(groups)
# Perform Tukey HSD test
tukey_result = pairwise_tukeyhsd(endog=values, groups=groups, alpha=0.05)
# Convert results to DataFrame and extract p-adj values
summary_df = pd.DataFrame(data=tukey_result.summary().data[1:], columns=tukey_result.summary().data[0])
p_adj_values = summary_df["p-adj"]
# Save p-adj values as a CSV in-memory file
csv_data = p_adj_values.to_csv(index=False, header=False)
# Define the destination blob name and upload the CSV data
blob = bucket.blob("p_adj_values.csv")
blob.upload_from_string(csv_data, content_type="text/csv")
# Return a response indicating the file was saved
return json.dumps({"message": "p-adj values saved to Google Cloud Storage"}), 200, {"Content-Type": "application/json"}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -in-google
Почему мой код Python не развертывается в Google? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение