转换结构化数组中的日期元素

2024-09-30 10:39:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我分析了一个数据文件,该文件的second元素为julian日期格式。在

array([(1957,  1, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999),
   (1957, 13, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999)],
  dtype=[('year', '<i4'), ('julian_date', '<i4'), ('1', '<i4'), ('2', '<i4'), ('3', '<i4'), ('4', '<i4'), ('5', '<i4'), ('6', '<i4'), ('7', '<i4'), ('8', '<i4'), ('9', '<i4'), ('10', '<i4'), ('11', '<i4'), ('12', '<i4')])

我只想将julian日期转换为月份,以便可视化我的数据。在

我知道如何使用julian_date元素执行简单的操作,例如:

^{pr2}$

我知道我可以使用datetime函数进行转换:

datetime.date(1956, 1, 1) + datetime.timedelta(121 - 1)

返回datetime.date(1956, 4, 30),其中4是月份号

但是,我不知道如何将此应用于我的数据。我对python和编程都是新手,如果有任何帮助,我将不胜感激。在

好吧。我想我已经接近.month方法了。当我手动输入yearjulian_date时:

x = datetime.date(1957, 1, 1) + datetime.timedelta(1 - 1) galveston['julian_date'] = x.month

它将元素更改为相应的月份:

array([(1957, 1, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999),(1957, 1, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999)],dtype=[('year', '<i4'), ('julian_date', '<i4'), ('1', '<i4'), ('2', '<i4'), ('3', '<i4'), ('4', '<i4'), ('5', '<i4'), ('6', '<i4'), ('7', '<i4'), ('8', '<i4'), ('9', '<i4'), ('10', '<i4'), ('11', '<i4'), ('12', '<i4')])

但是,他们都收到相同的月价值,这不是我需要做的。当我试图像这样修改函数时(顺便说一句,这可能是完全错误的):

x = datetime.date('year', 1, 1) + datetime.timedelta('julian_date' - 1) galveston['julian_date'] = x.month我有个错误。在

Image of the error


Tags: 数据函数元素datetimedate错误arrayyear
1条回答
网友
1楼 · 发布于 2024-09-30 10:39:40

我想我找到了自己问题的答案。我使用pandas库将一年中的日期转换为datetime64,然后我使用了.month方法,正如{a1}所建议的那样。在

# Import pandas library
import pandas as pd

# Create object x of datetime64 type
# Here I use galveston data array and to_datetime function
x = pd.to_datetime(galveston['year'] * 1000 + galveston['julian_date'], format='%Y%j')

# Here is the look at 20 dates from the array
x[20:40]
DatetimeIndex(['1957-08-29', '1957-09-10', '1957-09-22', '1957-10-04',
           '1957-10-16', '1957-10-28', '1957-11-09', '1957-11-21',
           '1957-12-03', '1957-12-15', '1957-12-27', '1958-01-01',
           '1958-01-13', '1958-01-25', '1958-02-06', '1958-02-18',
           '1958-03-02', '1958-03-14', '1958-03-26', '1958-04-07'],
          dtype='datetime64[ns]', freq=None)

# Get the month and put it back into galveston array
galveston['month'] = x.month

#The final result where the second element is the month
galveston[20:40]
array([(1957,  8, 1253, 1314, 1248, 1253, 1277, 1269, 1270, 1272, 1271, 1219, 1222, 1261),
   (1957,  9, 1284, 1284, 1277, 1258, 1292, 1349, 1309, 1428, 1439, 1261, 1271, 1344),
   (1957,  9, 1345, 1301, 1363, 1433, 1352, 1206, 1221, 1281, 1273, 1303, 1317, 1268),
   (1957, 10, 1172, 1177, 1307, 1319, 1280, 1215, 1166, 1208, 1281, 1384, 1510, 1598),
   (1957, 10, 1454, 1309, 1296, 1341, 1369, 1503, 1584, 1414, 1194, 1193, 1095, 1001),
   (1957, 10, 1060, 1111, 1159, 1141, 1184, 1193, 1140, 1205, 1287, 1278, 1348, 1148),
   (1957, 11, 1184, 1441, 1580, 1584, 1507, 1313, 1350, 1373, 1371, 1272, 1109, 1193),
   (1957, 11, 1284, 1365, 1199, 1064,  951, 1057, 1189, 1165, 1028,  666,  835,  996),
   (1957, 12, 1000,  929, 1116, 1253, 1084,  728,  625,  856,  617,  828, 1035, 1055),
   (1957, 12, 1011, 1069, 1090, 1105, 1142,  957,  949, 1127, 1180, 1089, 1090, 1083),
   (1957, 12, 1180, 1091, 1092, 1171, 1118, 9999, 9999, 9999, 9999, 9999, 9999, 9999),
   (1958,  1,  965, 1104, 1161, 1298, 1517, 1443, 1082,  879, 1011,  974, 1013, 1233),
   (1958,  1,  929,  823,  776,  675,  804,  873, 1159, 1158,  667,  784, 1218,  972),
   (1958,  1,  824,  888,  833,  914,  994,  964,  916,  641,  645,  780,  940,  986),
   (1958,  2,  928,  702,  833, 1071, 1117, 1020,  937,  910, 1079,  741,  757, 1082),
   (1958,  2, 1111, 1106, 1161, 1180, 1214, 1145,  964, 1008, 1125,  979,  841,  958),
   (1958,  3, 1061, 1016, 1112, 1237, 1146, 1142, 1119, 1011, 1070, 1169, 1191,  918),
   (1958,  3,  847,  924,  963, 1010,  937,  947,  907,  878,  950, 1050,  926,  936),
   (1958,  3,  954,  948, 1002, 1128, 1035,  995, 1111, 1164, 1140, 1147, 1231, 1127),
   (1958,  4,  980, 1221, 1315, 1033,  916, 1045, 1147, 1129,  942,  857,  946,  974)],
  dtype=[('year', '<i4'), ('month', '<i4'), ('1', '<i4'), ('2', '<i4'), ('3', '<i4'), ('4', '<i4'), ('5', '<i4'), ('6', '<i4'), ('7', '<i4'), ('8', '<i4'), ('9', '<i4'), ('10', '<i4'), ('11', '<i4'), ('12', '<i4')])

相关问题 更多 >

    热门问题