logo
Tags down

shadow

Blank data for certain hours - hence missing in my array. Is there a way to fill in the missing hours?


By : Saurabh Bhargava
Date : October 17 2020, 08:10 PM
help you fix your problem I have some data by the hour and summarise/consolidate the array. , I think this simplifies the logic and produces the result you want:
code :
const data = [
  {
    "date": "11/12/18 08:00",
    "count": 5
  },
  {
    "date": "11/12/18 07:00",
    "count": 4
  },
  {
    "date": "11/12/18 04:00",
    "count": 6
  }
];

let min = Number.MAX_VALUE;
let max = 0;

const dates = data.reduce((memo, {date, count}) => {
  const time = new Date(date).getTime();
  min = Math.min(min, time);
  max = Math.max(max, time);
  memo[time] = count;
  return memo;
}, {});

const ONE_HOUR = 60 * 60 * 1000;

const dataOutput = [];

for (let i = min;i <= max;i += ONE_HOUR) {
  dataOutput.push({
    date: new Date(i).toLocaleString(), 
    count: dates[i] || 0
  });
}

console.log(dataOutput)


Share : facebook icon twitter icon

MySQL how to fill missing hours/dates in range?


By : user3962547
Date : March 29 2020, 07:55 AM
help you fix your problem Not the prettiest. But it should do the trick if you really can't use temp tables:
code :
select ifnull(count,0) as count,dh.hour_of_day,
dh.day_of_week,date_format((date('2012-01-02') + interval dh.day_of_week day),'%W') as name_of_day
from
(
select day_of_week,hour_of_day
from 
(
 select 0 as day_of_week union select 1 union select 2 union select 3 
 union select 4 union select 5 union select 6
) d
 join
(
 select 0 as hour_of_day 
 union select 1 union select 2 union select 3 union select 4 
 union select 5 union select 6 union select 7 union select 8
 union select 9 union select 10 union select 11 union select 12
 union select 13 union select 14 union select 15 union select 16
 union select 17 union select 18 union select 19 union select 20
 union select 21 union select 22 union select 23
) h
) dh
left outer join
(
SELECT
count(id) as count,
HOUR(created) as hour_of_day,
WEEKDAY(created) as day_of_week,
DATE_FORMAT(created,'%W') name_of_day
FROM visitors
GROUP BY day_of_week,hour_of_day
) v on dh.day_of_week = v.day_of_week and dh.hour_of_day = v.hour_of_day
ORDER BY dh.day_of_week,dh.hour_of_day ASC; 

discontinious hourly data in datatable, possible to insert missing hours datetime value and null for datavalue?


By : Mirjalol G'aybullaye
Date : March 29 2020, 07:55 AM
To fix the issue you can do I wrote a piece of code that takes DataTable precip and inserts new rows with missing hour values. When the first column of the table has values 05:00, 07:00, 10:00; after the following operation, the output will be 05:00, 06:00, 07:00, 08:00, 09:00, 10:00.
I hope this is what you are looking for:
code :
        DataRow row;
        DateTime curRowDt, nextRowDt;
        object curObj, nextObj; // Used only for conversions

        for (int i = 0; i < precip.Rows.Count - 1; ++i)
        {
            // Resetting id numbers
            precip.Rows[i][2] = i;

            // Throws exception when changed to
            // curRowDt = (DateTime)precip.Rows[i][0];
            curObj = precip.Rows[i][0];
            nextObj = precip.Rows[i + 1][0];
            curRowDt = Convert.ToDateTime(curObj);
            nextRowDt = Convert.ToDateTime(nextObj);

            if (curRowDt.AddHours(1.0) != nextRowDt)
            {
                for (int j = 1; j < nextRowDt.Subtract(curRowDt).Hours; ++j)
                {
                    ++i;
                    row = precip.NewRow();
                    row.ItemArray = new object[] { curRowDt.AddHours(j), null, i };
                    precip.Rows.InsertAt(row, i);
                }
            }
        }
        // Resetting last row's id number
        precip.Rows[precip.Rows.Count - 1][2] = precip.Rows.Count - 1;

Aggregate and fill data for missing dates (days and hours)


By : Atul Barthwal
Date : March 29 2020, 07:55 AM
may help you . Suppose we have a list like this showing the count of each object on a particular date (mm-dd-yyyy-hour-minute): , What about this:
code :
from collections import defaultdict, OrderedDict                              
from datetime import datetime, timedelta                                      
from itertools import chain, groupby                                          

flat = sorted((datetime.strptime(d, '%m-%d-%Y-%H-%M').date(), f, c)           
              for (d, f, c) in chain(*A))                                     
counts = [(d, f, sum(e[2] for e in l))                                        
          for (d, f), l                                                       
          in groupby(flat, key=lambda t: (t[0], t[1]))]                       

# lets assume that there are some data                                        
start = counts[0][0]                                                          
end = counts[-1][0]                                                           
result = OrderedDict((start+timedelta(days=i), defaultdict(int))             
                     for i in range((end-start).days+1))                      
for day, data in groupby(counts, key=lambda d: d[0]):                         
    result[day].update((f, c) for d, f, c in data)
from collections import defaultdict                                           
from datetime import datetime, timedelta                                      
from itertools import chain, groupby                                          

def aggregate(data, resolution='daily'):                                      
    assert resolution in ['hourly', 'daily']                                  
    if resolution == 'hourly':                                                
        round_dt = lambda dt: dt.replace(minute=0, second=0, microsecond=0)   
    else:                                                                     
        round_dt = lambda dt: dt.date()                                       

    flat = sorted((round_dt(datetime.strptime(d, '%m-%d-%Y-%H-%M')), f, c)    
                  for (d, f, c) in chain(*A))                                 
    counts = [(d, f, sum(e[2] for e in l))                                    
              for (d, f), l                                                   
              in groupby(flat, key=lambda t: (t[0], t[1]))]
    result = {}                                                              
    for day, data in groupby(counts, key=lambda d: d[0]):                    
        d = result[day] = defaultdict(int)                                   
        d.update((f, c) for d, f, c in data)                                 
    return result                                                            

def xaggregate(data, resolution='daily'):                                      
    aggregated = aggregate(data, resolution)                                 
    curr = min(aggregated.keys())                                            
    end = max(aggregated.keys())                                             
    interval = timedelta(days=1) if resolution == 'daily' else timedelta(seconds=3600)
    while curr <= end:
        # None is sensible value in case of missing data I think                                                       
        yield curr, aggregated.get(curr)                   
        curr += interval                                                                                 

Fill in missing hours in a pandas dataframe


By : user1808750
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have a dataframe that contains hourly data: , You may check resample with groupby
code :
df['Datetime']=pd.to_datetime(df.date+' '+df.hour)# combine hour and date to datetime 

df.drop(['date','hour'],inplace=True,axis = 1)# drop duplicate infomation
df.groupby('area').\
    apply(lambda x : x.set_index('Datetime').resample('H').mean().fillna(0)).\
      reset_index()
Out[662]: 
  area            Datetime  output
0   H1 2018-07-01 07:00:00   150.0
1   H1 2018-07-01 08:00:00   150.0
2   H1 2018-07-01 09:00:00   100.0
3   H1 2018-07-01 10:00:00     0.0
4   H1 2018-07-01 11:00:00   150.0
5   H2 2018-07-01 09:00:00   100.0
6   H2 2018-07-01 10:00:00    50.0
7   H2 2018-07-01 11:00:00    50.0
8   H2 2018-07-01 12:00:00   150.0

Fill missing dates-hours on dataframe with empty values


By : user2821427
Date : March 29 2020, 07:55 AM
it helps some times You can use resample here:
code :
# df['DATE'] = pd.to_datetime(df['DATE'])
# df['HOUR'] = pd.to_timedelta(df['HOUR'])

grp = df.set_index(df['DATE']+df['HOUR']).groupby(['MARKET_ID', 'SECTOR'], 
                                                  sort=False).resample('H').sum().reset_index(level=1)
                               SECTOR  MARKET_ID  REVENUE  COUPONS  ITEMS
MARKET_ID                                                                
21        2019-02-24 15:00:00  Fruits         21   808.60       19     29
          2019-02-24 16:00:00  Fruits          0     0.00        0      0
          2019-02-24 17:00:00  Fruits          0     0.00        0      0
          2019-02-24 18:00:00  Fruits          0     0.00        0      0
          2019-02-24 19:00:00  Fruits          0     0.00        0      0
          2019-02-24 20:00:00  Fruits          0     0.00        0      0
          2019-02-24 21:00:00  Fruits          0     0.00        0      0
          2019-02-24 22:00:00  Fruits         21   267.54        8      8
          2019-02-24 23:00:00  Fruits          0     0.00        0      0
          2019-02-25 00:00:00  Fruits          0     0.00        0      0
          2019-02-25 01:00:00  Fruits          0     0.00        0      0
          2019-02-25 02:00:00  Fruits          0     0.00        0      0
          2019-02-25 03:00:00  Fruits          0     0.00        0      0
          2019-02-25 04:00:00  Fruits          0     0.00        0      0
          2019-02-25 05:00:00  Fruits          0     0.00        0      0
          2019-02-25 06:00:00  Fruits          0     0.00        0      0
          2019-02-25 07:00:00  Fruits          0     0.00        0      0
          2019-02-25 08:00:00  Fruits          0     0.00        0      0
          2019-02-25 09:00:00  Fruits          0     0.00        0      0
          2019-02-25 10:00:00  Fruits          0     0.00        0      0
          2019-02-25 11:00:00  Fruits          0     0.00        0      0
          2019-02-25 12:00:00  Fruits          0     0.00        0      0
          2019-02-25 13:00:00  Fruits          0     0.00        0      0
          2019-02-25 14:00:00  Fruits          0     0.00        0      0
          2019-02-25 15:00:00  Fruits          0     0.00        0      0
          2019-02-25 16:00:00  Fruits          0     0.00        0      0
          2019-02-25 17:00:00  Fruits          0     0.00        0      0
          2019-02-25 18:00:00  Fruits          0     0.00        0      0
          2019-02-25 19:00:00  Fruits          0     0.00        0      0
          2019-02-25 20:00:00  Fruits          0     0.00        0      0
          2019-02-25 21:00:00  Fruits          0     0.00        0      0
          2019-02-25 22:00:00  Fruits          0     0.00        0      0
          2019-02-25 23:00:00  Fruits          0     0.00        0      0
          2019-02-26 00:00:00  Fruits          0     0.00        0      0
          2019-02-26 01:00:00  Fruits          0     0.00        0      0
          2019-02-26 02:00:00  Fruits          0     0.00        0      0
          2019-02-26 03:00:00  Fruits          0     0.00        0      0
          2019-02-26 04:00:00  Fruits          0     0.00        0      0
          2019-02-26 05:00:00  Fruits          0     0.00        0      0
          2019-02-26 06:00:00  Fruits          0     0.00        0      0
          2019-02-26 07:00:00  Fruits          0     0.00        0      0
          2019-02-26 08:00:00  Fruits          0     0.00        0      0
          2019-02-26 09:00:00  Fruits          0     0.00        0      0
          2019-02-26 10:00:00  Fruits          0     0.00        0      0
          2019-02-26 11:00:00  Fruits          0     0.00        0      0
          2019-02-26 12:00:00  Fruits          0     0.00        0      0
          2019-02-26 13:00:00  Fruits          0     0.00        0      0
          2019-02-26 14:00:00  Fruits          0     0.00        0      0
          2019-02-26 15:00:00  Fruits          0     0.00        0      0
          2019-02-26 16:00:00  Fruits          0     0.00        0      0
...                               ...        ...      ...      ...    ...
          2019-03-01 14:00:00   Dairy          0     0.00        0      0
          2019-03-01 15:00:00   Dairy          0     0.00        0      0
          2019-03-01 16:00:00   Dairy          0     0.00        0      0
          2019-03-01 17:00:00   Dairy          0     0.00        0      0
          2019-03-01 18:00:00   Dairy          0     0.00        0      0
          2019-03-01 19:00:00   Dairy          0     0.00        0      0
          2019-03-01 20:00:00   Dairy          0     0.00        0      0
          2019-03-01 21:00:00   Dairy          0     0.00        0      0
          2019-03-01 22:00:00   Dairy          0     0.00        0      0
          2019-03-01 23:00:00   Dairy          0     0.00        0      0
          2019-03-02 00:00:00   Dairy          0     0.00        0      0
          2019-03-02 01:00:00   Dairy          0     0.00        0      0
          2019-03-02 02:00:00   Dairy          0     0.00        0      0
          2019-03-02 03:00:00   Dairy          0     0.00        0      0
          2019-03-02 04:00:00   Dairy          0     0.00        0      0
          2019-03-02 05:00:00   Dairy          0     0.00        0      0
          2019-03-02 06:00:00   Dairy          0     0.00        0      0
          2019-03-02 07:00:00   Dairy          0     0.00        0      0
          2019-03-02 08:00:00   Dairy          0     0.00        0      0
          2019-03-02 09:00:00   Dairy          0     0.00        0      0
          2019-03-02 10:00:00   Dairy          0     0.00        0      0
          2019-03-02 11:00:00   Dairy          0     0.00        0      0
          2019-03-02 12:00:00   Dairy          0     0.00        0      0
          2019-03-02 13:00:00   Dairy          0     0.00        0      0
          2019-03-02 14:00:00   Dairy          0     0.00        0      0
          2019-03-02 15:00:00   Dairy          0     0.00        0      0
          2019-03-02 16:00:00   Dairy          0     0.00        0      0
          2019-03-02 17:00:00   Dairy          0     0.00        0      0
          2019-03-02 18:00:00   Dairy          0     0.00        0      0
          2019-03-02 19:00:00   Dairy          0     0.00        0      0
          2019-03-02 20:00:00   Dairy          0     0.00        0      0
          2019-03-02 21:00:00   Dairy          0     0.00        0      0
          2019-03-02 22:00:00   Dairy          0     0.00        0      0
          2019-03-02 23:00:00   Dairy          0     0.00        0      0
          2019-03-03 00:00:00   Dairy          0     0.00        0      0
          2019-03-03 01:00:00   Dairy          0     0.00        0      0
          2019-03-03 02:00:00   Dairy          0     0.00        0      0
          2019-03-03 03:00:00   Dairy          0     0.00        0      0
          2019-03-03 04:00:00   Dairy          0     0.00        0      0
          2019-03-03 05:00:00   Dairy          0     0.00        0      0
          2019-03-03 06:00:00   Dairy          0     0.00        0      0
          2019-03-03 07:00:00   Dairy          0     0.00        0      0
          2019-03-03 08:00:00   Dairy          0     0.00        0      0
          2019-03-03 09:00:00   Dairy          0     0.00        0      0
          2019-03-03 10:00:00   Dairy          0     0.00        0      0
          2019-03-03 11:00:00   Dairy          0     0.00        0      0
          2019-03-03 12:00:00   Dairy          0     0.00        0      0
          2019-03-03 13:00:00   Dairy          0     0.00        0      0
          2019-03-03 14:00:00   Dairy          0     0.00        0      0
          2019-03-03 15:00:00   Dairy         21   122.69        4      4

[180 rows x 5 columns]
Related Posts Related Posts :
  • *NgFor toggle show/hide individual element
  • Add placeholder to multiple google chart string filters
  • Get value from radio button Angular Material Dialog
  • Use mergeWith of Lodash with nested objects
  • with javascript function created select onchange with counter parameter
  • SyntaxError using spread operator in webpack.config.js
  • Express - push to testing array on post request
  • Why should I use Interfaces in Typescript
  • What is a best way of detecting change of array?
  • Images are not getting the Div height when loading in jQuery
  • Find element by HTML in Cypress
  • Travsering table and storing values as key/value pair
  • Angular Material Checkbox doesn´t change state of checked
  • copy props from obj to obj in a functional Programming way
  • Bubble chart with NextJS and highcharts-react-official
  • Update value in table from Dialog Angular MAterial
  • ReferenceError because the variable is not being passed on
  • RequiredJS failed to Load a JS file
  • How can I disable the Stripe payment request button until a form is complete?
  • How to show a element immediately, such as a loading indicator, with Vue? nextTick isn't working as I expect
  • v-on:click with target not in the correct element?
  • Get last occurrence of item in array of arrays (Javascript)
  • Event binding with $(window) on dynamically created AJAX elements
  • How to use a variable in a custom function in Twilio Studio?
  • Prevent textarea from automatically scrolling when I change cursor position
  • Headless browser in Azure functions JavaScript?
  • Javascript to "fix" all hrefs of class with regex
  • How do I pass a value to list-item
  • Using component in entry component?
  • Working with different timezones in Javascript
  • Does MobX Observables have anything to do with RxJS ones?
  • Google extension how to inject my script to active tab and link with pop-up button?
  • Removing duplicate keys from object array
  • One @click event with multiple toggles
  • How to add TextToSpeech.talk("hi') action to a button
  • Not able to render vanilla webcomponent imported as template
  • How can I remove Materialize's select dropdown when using select2?
  • react-loadable multiple resource loading not working
  • Making SharePoint Lookup field multi selection in code with PNP/SP
  • input equal to a string for one array, and get string from same position from another array - JavaScript
  • Proper Method for Storing JavaScript Instances in Nan::ObjectWrap C++ Class
  • jQuery Calculate remaining characters length for input
  • Highcharts transform JSON into data without losing informations
  • How can I validate my file input name using regex?
  • Moment js interval for a day in 12 hours format
  • What would be the implementation of the function in this scenario
  • How to get text next a input tag using document.querySelector?
  • What could be reason of the error 'Maximum call stack size exceeded' inside Ember Data?
  • Leaflet polyline precision loss on zoom out
  • Convert Javascript regex to Typescript regex
  • Textext.js - Remove all tags and clean textarea
  • Array splice with unexpected results
  • Having trouble with multiple active states in ReactJS
  • Error 500 for some Ajax requests on server
  • Event call at the end of HTTP call
  • wooommerce add to cart based on gravity form radio button
  • Angular 6: table with *ngIF generates one <td> more than i need
  • how to load an external script with react?
  • Getting current stored value from database in a form on Cascading dropdowns in edit mode C# MVC
  • Best Way to Pass Query Parameters to URL Using Axios in Vue?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org