将日期时间 '%d-%m-%Y %H:%M' 配置为 '%d/%m/%Y %H:%M' 到一行代码中

Configurate datetime '%d-%m-%Y %H:%M' to '%d/%m/%Y %H:%M' into one-line code

我有以下数据:

pd_mean['Date_Time'] = pd.to_datetime(pd_mean['Date_Time'])

数据:

 Date_Time   GHI     DNI   DIF  flagR       SE       SA    TEMP        AP      RH     WS      WD    PWAT
0      1994-01-01 00:00:00   0.0    0.00   0.0   0.00 -39.7850 -25.6125  14.300  1004.300  93.600  0.150   97.25  17.925
1      1994-01-01 01:00:00   0.0    0.00   0.0   0.00 -32.2700 -39.9425  14.100  1004.050  94.575  0.375  248.50  18.525
2      1994-01-01 02:00:00   0.0    0.00   0.0   0.00 -22.4050 -50.8275  13.650  1004.200  97.050  0.750  298.50  18.750
3      1994-01-01 03:00:00   0.0    0.00   0.0   0.00 -11.0400 -59.0600  13.725  1004.450  98.875  1.075  271.50  18.900
4      1994-01-01 04:00:00  40.5  118.25  29.5   1.75   1.7800 -65.4425  13.625  1004.900  98.725  0.950  252.00  18.975
...                    ...   ...     ...   ...    ...      ...      ...     ...       ...     ...    ...     ...     ...
244891 2021-12-08 19:00:00   0.0    0.00   0.0   0.00 -38.4000  91.2925  11.350  1009.600  76.050  3.700  182.00  10.900
244892 2021-12-08 20:00:00   0.0    0.00   0.0   0.00 -52.2175  84.9725  11.050  1010.225  77.175  3.475  183.25  10.900
244893 2021-12-08 21:00:00   0.0    0.00   0.0   0.00 -65.8375  74.8725  10.750  1010.300  78.300  3.350  184.75  10.800
244894 2021-12-08 22:00:00   0.0    0.00   0.0   0.00 -78.0150  46.2400  10.350  1010.075  80.450  2.900  183.00  10.600
244895 2021-12-08 23:00:00   0.0    0.00   0.0   0.00 -79.8725 -32.1000   9.900  1009.800  82.725  2.450  185.25  10.200

稍后我尝试为每年制作一个 csv 文件。我的代码有效。

pd_mean['Date_Time'] = pd.to_datetime(pd_mean['Date_Time'])
    
for year_X in range(years.min(), years.max()+1):
        print(f"Creating file (1 hr) for the year: {year_X}") 
        print(pd_mean[years == year_X]
        pd_mean[years == year_X].to_csv(f"data_(1hr)_year_{year_X}.csv")

每年的数据:(例如:1994 年)

Creating file (1 hr) for the year: 1994
               Date_Time   GHI     DNI   DIF  flagR       SE       SA    TEMP        AP      RH     WS      WD    PWAT
0    1994-01-01 00:00:00   0.0    0.00   0.0   0.00 -39.7850 -25.6125  14.300  1004.300  93.600  0.150   97.25  17.925
1    1994-01-01 01:00:00   0.0    0.00   0.0   0.00 -32.2700 -39.9425  14.100  1004.050  94.575  0.375  248.50  18.525
2    1994-01-01 02:00:00   0.0    0.00   0.0   0.00 -22.4050 -50.8275  13.650  1004.200  97.050  0.750  298.50  18.750
3    1994-01-01 03:00:00   0.0    0.00   0.0   0.00 -11.0400 -59.0600  13.725  1004.450  98.875  1.075  271.50  18.900
4    1994-01-01 04:00:00  40.5  118.25  29.5   1.75   1.7800 -65.4425  13.625  1004.900  98.725  0.950  252.00  18.975
...                  ...   ...     ...   ...    ...      ...      ...     ...       ...     ...    ...     ...     ...
8755 1994-12-31 19:00:00   0.0    0.00   0.0   0.00 -20.6150  52.2200  19.525  1003.950  74.950  4.900  198.25  11.350
8756 1994-12-31 20:00:00   0.0    0.00   0.0   0.00 -30.7475  41.8250  18.125  1004.525  78.800  4.175  195.25  11.725
8757 1994-12-31 21:00:00   0.0    0.00   0.0   0.00 -38.7000  28.0950  17.175  1005.025  82.150  3.500  195.50  11.950
8758 1994-12-31 22:00:00   0.0    0.00   0.0   0.00 -43.3450  10.8950  17.150  1004.925  84.400  2.775  198.75  12.125

但是,我意识到我需要 %d/%m/%Y %H:%M 格式。当我尝试添加格式时出现错误。

新添加的代码:

for year_X in range(years.min(), years.max()+1):
    print(f"Creating file (1 hr) for the year: {year_X}")
    print(pd_mean[years == year_X]
    pd_mean[years == year_X]dt.strftime('%d/%m/%Y %H:%M').to_csv(f"data_(1hr)_year_{year_X}.csv") 

错误:

pd_mean[years == year_X].dt.strftime('%d/%m/%Y %H:%M').to_csv(f"data_(1hr)_year_{year_X}.csv")

AttributeError: 'DataFrame' object has no attribute 'dt

'

试试这个:

df_subset = pd_mean[years == year_X]
df_subset['Date_Time'] = df_subset['Date_Time'].dt.strftime('%d/%m/%Y %H:%M')
df_subset.to_csv(f"data_(1hr)_year_{year_X}.csv")