Home > Software engineering >  python extract date and hour value from dict object in array
python extract date and hour value from dict object in array

Time:12-29

I want to extract date and hour value (e.g. 2013-10-04 11, 2013-10-04 04, 2013-10-04 03 etc). When I use keys(), then I am able to get 2013-10-04 not hour and when using items(), then all data extracted. If you know the way to extract date and hour value, please let me know. Additionally, the data has 30 min intervals.

u'Station_paris_2013-10-04': {'2013-10-04 11:00:00': array([ number,   number,   number, ...,
        number, number, number]), '2013-10-04 04:00:00': array([ number,   number, number, ...,
         number, number, number]), '2013-10-04 03:00:00': array([ number, number, number, ...,
        number, number, number]), '2013-10-04 14:30:00': array([ number, number, number, ...,
        number, number, number]), '2013-10-04 20:00:00': array([ number, ....]...)

Sorry for confusing..

This is the code for export_allcorr2 and use the allcorr[ccfid] as data.

 def export_allcorr2(session, ccfid, data):
        output_folder = get_config(session, 'output_folder')
        station1, station2, components, filterid, date = ccfid.split('_')
    
        path = os.path.join(output_folder, "i" % int(filterid),
                            station1, station2, components)
        if not os.path.isdir(path):
            os.makedirs(path)
    df = pd.DataFrame().from_dict(data).T
    df.columns = get_t_axis(session)
    df.to_hdf(os.path.join(path, date '.h5'), 'data')
    del df
    return



if params.keep_all:
   for ccfid in allcorr.keys():
       export_allcorr2(db, ccfid, allcorr[ccfid])

This is a part of the file (allcorr) for one station pair that I am using,

'2013-10-27 10:30:00': array([ 583.55720165,  424.74395062,  244.40351166, ...,  244.40364883,
        424.74411523,  583.55747599]), '2013-10-27 16:30:00': array([ 199.66430727,   18.39147977, -157.45584362, ..., -157.45602195,
         18.39139403,  199.66432099]), '2013-10-27 16:00:00': array([ -97.27305213, -365.27786008, -621.36060357, ..., -621.36076818,
       -365.27802469,  -97.27297668]), '2013-10-27 21:30:00': array([-436.08005487, -389.74776406, -327.61319616, ..., -327.61300412,
       -389.74773663, -436.07994513]), '2013-10-27 11:00:00': array([-649.70282579, -597.36164609, -523.04197531, ..., -523.04170096,
       -597.36131687, -649.70266118]), '2013-10-27 20:30:00': array([ 347.37681756,  218.49106996,   88.03422497, ...,   88.03427298,
        218.49113855,  347.37687243]), '2013-10-27 12:30:00': array([  34.91324417,  -93.73432099,  171.31466392, ...,  171.31396433,
        -93.73384088,   34.91361454]), '2013-10-27 13:30:00': array([-289.4951989 , -404.48175583, -501.02052126, ..., -501.02046639,
       -404.48170096, -289.49500686]), '2013-10-27 07:30:00': array([-108.69506859,  -44.65974623,    7.96771948, ...,    7.96728738,
        -44.65979424, -108.69509602]), '2013-10-27 09:30:00': array([-630.18035665, -614.95835391, -597.89119342, ..., -597.89113855,
       -614.95807956, -630.18024691]), '2013-10-27 17:00:00': array([-276.81805213, -267.21061728, -246.72584362, ..., -246.72556927,
       -267.21053498, -276.81794239]),

ccfid shows u'05.SS08_05.SS09_ZZ_01_2013-11-06' which is 'Net.Sta_Net.Sta_component_ccfid_date'. But what I want is date with hour.

This is allcorr[ccfid].

 {'2013-11-07 07:30:00': array([  2.01912938e-08,  -5.87221879e-08,   7.99213765e-08, ...,
     9.93437383e-08,   4.46988525e-08,  -4.40811423e-08]), '2013-11-07 14:30:00': array([ -7.76317889e-09,   1.72162791e-09,   1.76833389e-08, ...,
    -4.17227052e-08,  -8.08114523e-09,   7.22184605e-09]), '2013-11-07 00:00:00': array([ -1.67720752e-08,  -4.86950919e-08,  -3.92029027e-08, ...,
    -4.25311992e-08,  -1.43883637e-08,  -1.86576377e-08]), '2013-11-07 16:00:00': array([ -1.54196405e-08,  -6.50798506e-08,  -3.71392759e-08, ...,
    -3.63095301e-08,   4.17709433e-10,  -1.11803857e-07]), '2013-11-07 15:30:00': array([ -4.30306800e-08,  -8.02815645e-08,   1.83716952e-08, ...,
    -3.71510132e-08,  -5.32969688e-08,   5.72185107e-08])

And in the export_allcorr2code, what I want is to make the data file format from Y-M-D to Y-M-D-H or Y-M-D H format. So extract the H (hour) data, and join the files of same station pair, date, and hour.

CodePudding user response:

You can use datetime to extract day, hour etc.

inp = {'2013-10-04 11:00:00':["rand_stuff"],
      '2013-10-04 04:00:00':["rand_stuff"]}
from datetime import datetime
for ts in inp.keys():
    dt = datetime.strptime(ts,"%Y-%m-%d %H:%M:%S")
    print(f"Date: {dt} Hour: {dt.hour} Day: {dt.day}") 


Date: 2013-10-04 11:00:00 Hour: 11 Day: 4
Date: 2013-10-04 04:00:00 Hour: 4 Day: 4

CodePudding user response:

I cannot comment yet then bear it. Please add details about your dict data and post it the way that each element is in a row.

Do that then I will edit answer OK!

  • Related