jsdataframe
Version:
a data frame library inspired by R and Python Pandas
2 lines • 51.2 kB
JavaScript
!function(t,e){"function"==typeof define&&define.amd?define(["exports"],e):e("object"==typeof exports&&"string"!=typeof exports.nodeName?exports:t.jsdataframe={})}(this,function(t){"use strict";function e(t){return String.fromCharCode(t)}function r(t,e,r){var n,i=r.size(),o=t.length-e,u=st(i);for(n=0;i>n;n++)u[n]=st(o);for(var s=0;o>s;s++){var a=t[s+e];if(a.length!==i)throw new Error("all row arrays must be of the same size");for(n=0;i>n;n++)u[n][s]=a[n]}return gt(u,r)}function n(t){return Jt.vector(t.values,t.dtype)}function i(t){return 0!==t&&Math.abs(t)<ye}function o(t){return Math.abs(t)>ge}function u(t){var e=t.nChar().max(),r=Jt.rep(" ",e).strJoin("");return t.map(function(t){return(r+t).slice(-r.length)})}function s(t,e){var r=Jt.seq(t)._toTruncatedPrintVector(e);return r.map(function(t){return t===we?t:t+_e})}function a(t){if(Ht(t))return"undefined";if(null===t)return"null";if(Number.isNaN(t))return"NaN";var e=Et(t),r=e.split("\n",2);return r.length>1&&(e=r[0]+"..."),e.length>pe&&(e=e.slice(0,pe-3)+"..."),e}function l(t,e,r){if("number"!=typeof t||Number.isNaN(t))throw new Error('"'+r+'" must be a number');if(e>t)throw new Error('"'+r+'" too small')}function c(t){var e=t.toString().split(".");return e.length>1?e[1].length:0}function h(t,e){e=Ht(e)?!0:e;var r=t.dtype;"date"===t.dtype&&(t=t.toDtype("number"));var n="number"!==t.dtype?t.values.slice():t.values.map(function(t){return Number.isNaN(t)?null:t}),i={dtype:r,values:n};return e&&(i.version=Jt.version,i.type=Gt.type),i}function f(t){return!ut(t)}function v(t,e,r,n){return ut(e)?n:e?t:r}function p(t,e){return 0===Ct(t,e)}function d(t,e){return Number.isNaN(t)&&Number.isNaN(e)||t===e}function m(t,e){return Ct(t,e)<0}function y(t,e){return Ct(t,e)>0}function g(t,e){return Number.isNaN(t)&&Number.isNaN(e)||Math.abs(t-e)<=1e-7}function b(t,e){return Ct(e,t)<0?e:t}function w(t,e){return Ct(e,t)>0?e:t}function _(t,e,r){var n=ut(e),i=ut(r);if(!n&&!i&&e>r)throw new Error("invalid range: lower ("+e+") > upper ("+r+")");return ut(t)?t:!n&&e>t?e:!i&&t>r?r:t}function x(t,e){return t+e}function N(t,e){return t-e}function E(t,e){return t*e}function O(t,e){return t/e}function j(t,e){return t%e}function C(t,e){return t.n++,t.sum+=e,t}function A(t,e){t.n++;var r=e-t.mean;return t.mean+=r/t.n,t.M2+=r*(e-t.mean),t}function z(t,e){return t===!1||e===!1?!1:null===t||null===e?null:!0}function M(t,e){return t===!0||e===!0?!0:null===t||null===e?null:!1}function S(t){return!t}function k(t,e){return null===t||null===e?null:t!==e}function I(t,e){return+t+ +e}function R(t){return t.length}function V(t,e,r){return null===t?null:t.includes(e,r)}function q(t,e,r){return null===t?null:t.startsWith(e,r)}function L(t,e,r){return null===t?null:t.endsWith(e,r)}function T(t,e,r){return null===t?NaN:t.indexOf(e,r)}function D(t,e,r){return null===t?NaN:t.lastIndexOf(e,r)}function F(t,e){return null===t?NaN:t.search(e)}function B(t,e){return null===e||null===t?null:e.test(t)}function W(t){return null===t?null:t.toLowerCase()}function K(t){return null===t?null:t.toUpperCase()}function U(t){return function(){var e=t.apply(null,arguments);if(null===e)return e;var r=this[e];return Ht(r)&&(this[e]=e,r=e),r}}function P(t,e,r,n,i,o,u,s){var a,l,c=s?o:i,h=0;for(l=e.nCol(),a=0;l>a;a++)t[h].push(e._cols[a].values[c]),h++;for(l=r.nCol(),a=0;l>a;a++){var f=null===i?null:r._cols[a].values[i];t[h].push(f),h++}for(l=n.nCol(),a=0;l>a;a++){var v=null===o?null:n._cols[a].values[o];t[h].push(v),h++}h<t.length&&t[h].push(u)}function Y(t,e,r){var n,i;if(Ht(r)||Ht(r.by)&&Ht(r.leftBy)&&Ht(r.rightBy)){var o=t.names().s(t.names().isIn(e.names()));if(o.nUnique()!==o.size())throw new Error("duplicate names found for key columns");n=J(o,t._names,t._dtypesVector()),i=J(o,e._names,e._dtypesVector())}else if(Ht(r.by)){if(Ht(r.leftBy)||Ht(r.rightBy))throw new Error("must specify both opts.leftBy and "+"opts.rightBy together");n=J(r.leftBy,t._names,t._dtypesVector()),i=J(r.rightBy,e._names,e._dtypesVector())}else{if(!Ht(r.leftBy)||!Ht(r.rightBy))throw new Error("cannot define opts.by, opts.leftBy, and "+"opts.rightBy all together");if(null!==r.by&&"object"==typeof r.by&&r.by.type!==Gt.type){var u=Object.keys(r.by),s=u.map(function(t){return r.by[t]});n=J(u,t._names,t._dtypesVector()),i=J(s,e._names,e._dtypesVector())}else{var a=Bt(r.by,"string");if(n=J(a,t._names,t._dtypesVector()),i=J(a,e._names,e._dtypesVector()),a.size()!==n.size())throw new Error("duplicate names found for key columns")}}if(yt(n),yt(i),n.size()!==i.size())throw new Error("must select the same number of key columns on the "+"left and right side; instead got "+n.size()+", "+i.size());if(0===n.size()||0===i.size())throw new Error("must select at least one key column to join by");return{left:n,right:i}}function H(t,e){if(Ht(t)||null===t)return null;var r=G(t,e);if(!Ht(r))return r;var n=X(Se.INT,e);return Z(t,n)}function J(t,e,r){if(Ht(t)||null===t)return null;var n=e.size(),i=G(t,n);if(!Ht(i))return i;var o=X(Se.COL,n,e._getIndex(),r);return Z(t,o)}function G(t,e){if(t.type===ne.type){var r=G(t._selector,e);return Ht(r)?void 0:Q(r,e)}if(Array.isArray(t)&&(t=_t(t.slice(),"object")),t.type===Gt.type&&"boolean"===t.dtype){if(t.size()!==e)throw new Error("inappropriate boolean indexer length ("+t.size()+"); expected length to be "+e);return t.which()}}function Q(t,e){return Jt.seq(e).isIn(t).not().which()}function X(t,e,r,n){return r=Ht(r)?null:r,n=Ht(n)?null:n,{resolveMode:t,maxLen:e,index:r,dtypes:n}}function Z(t,e){var r=[];return $(t,e,r),at(r,"number")}function $(t,e,r){if(null!==t&&"object"==typeof t){if("function"==typeof t._resolveSelectorHelper)return void t._resolveSelectorHelper(e,r);if(Array.isArray(t)){for(var n=0;n<t.length;n++)$(t[n],e,r);return}}var i;switch(e.resolveMode){case Se.INT:r.push(et(t,e.maxLen));break;case Se.COL:if(Kt(t))r.push(et(t,e.maxLen));else{if(!Pt(t)&&null!==t)throw new Error("expected integer or string selector but got: "+t);i=e.index.lookupKey([t]),rt(i,t,e,r)}break;case Se.KEY:if(1!==e.index.arity)throw new Error("unimplemented case (TODO)");var o=e.index.initVectors[0].dtype;Rt(t,o),i=e.index.lookupKey([t]),rt(i,t,e,r);break;default:throw new Error("Unrecognized RESOLVE_MODE: "+e.resolveMode)}}function tt(t,e,r,n){var i;if(Ht(t))return r?e.maxLen:0;var o=e.resolveMode===Se.INT||e.resolveMode===Se.COL&&"number"==typeof t;if(null===n&&(n=!o),o)return i=et(t,e.maxLen,!1),r&&n?i+1:i;if(1===e.index.arity){if(i=e.index.lookupKey([t]),null===i)throw new Error("could not find entry for range bound: "+t);return"number"==typeof i?r&&n?i+1:i:r&&n?i[i.length-1]+1:i[0]}throw new Error("unimplemented case (TODO)")}function et(t,e,r){if(Ht(r)&&(r=!0),!Number.isInteger(t))throw new Error("expected integer selector for integer indexing "+"but got non-integer: "+t);var n=0>t?e+t:t;if(r&&(0>n||n>=e))throw new Error("integer index out of bounds");return n}function rt(t,e,r,n){if(null===t)throw r.resolveMode===Se.COL?new Error('could not find column named "'+e+'"'):new Error("could find entry for key: "+e);if("number"==typeof t)n.push(t);else for(var i=0;i<t.length;i++)n.push(t[i])}function nt(t,e){if(Ht(t))throw new Error("selector must not be undefined");if(t=Ft(t),Kt(t))return et(t,e.values.length);if(Pt(t)||null===t){var r=e._getIndex().lookupKey([t]);if(null===r)throw new Error("invalid column name: "+t);return"number"!=typeof r&&(r=r[0]),r}throw new Error("column selector must be an integer or string")}function it(t,e){return null===t?ke["null"]:Ht(t)?ke.undefined:"date"===e?t.valueOf():t}function ot(t){var e=t.length;if(0===e)throw new Error("cannot index an empty list of vectors");var r=t[0].size(),n=Object.create(oe);n.initVectors=t,n.size=0,n.arity=e,n._dtypes=t.map(function(t){return t.dtype}),n._map=Object.create(null);for(var i=n._map,o=e-1,u=0;r>u;u++){for(var s,a,l=i,c=0;o>c;c++){s=t[c],a=it(s.values[u],s.dtype);var h=l[a];Ht(h)&&(l[a]=h=Object.create(null)),l=h}s=t[o],a=it(s.values[u],s.dtype);var f=l[a];Ht(f)?(l[a]=u,n.size++):"number"==typeof f?l[a]=[f,u]:f.push(u)}return n}function ut(t){return null===t||Ht(t)||Number.isNaN(t)}function st(t){return 64e3>t?new Array(t):[]}function at(t,e){Mt(e);var r=ae[e],n=Object.create(r);return n._init(t),n}function lt(t,e,r){for(var n=t.length,i=st(n),o=0;n>o;o++){var u=t[o];i[o]=ut(u)?e:r(u)}return i}function ct(t,e,r){for(var n=e,i=0;i<t.length;i++){var o=t[i];ut(o)||(n=r(n,o))}return n}function ht(t,e,r,n){for(var i=e,o=0;o<t.length;o++){var u=t[o];if(r(u))return u;i=n(i,u)}return i}function ft(t,e,r,n){var i=!0;Ht(n)&&(n=r,i=!1);for(var o=t.length,u=e.length,s=Dt(o,u),a=1===o,l=1===u,c=st(s),h=0;s>h;h++){var f=a?t[0]:t[h],v=l?e[0]:e[h];i&&(ut(f)||ut(v))?c[h]=r:c[h]=n(f,v)}return c}function vt(t,e,r){if(0===t.length)throw new Error("cannot combine an empty list of arrays");r=Ht(r)?null:r;var n,i=t.length,o=st(i),u=1;for(n=0;i>n;n++)if(1===t[n].length)o[n]=!0;else if(o[n]=!1,1===u)u=t[n].length;else if(u!==t[n].length)throw new Error("length mismatch: expected "+u+" elements but found "+t[n].length);for(var s=st(u),a=st(i),l=0;u>l;l++){for(n=0;i>n;n++)a[n]=o[n]?t[n][0]:t[n][l];s[l]=e.apply(r,a)}return s}function pt(t,e,r){var n=!1;Ht(r)&&(r=e,n=!0);for(var i=t.length,o=st(i),u=null,s=!1,a=0;i>a;a++){var l=t[a];if(ut(l)){if(o[a]=l,!n){for(var c=a+1;i>c;c++)o[c]=e;break}}else u=s?r(u,l):l,s=!0,o[a]=u}return o}function dt(t,e){for(var r=st(e.length),n=0;n<e.length;n++)r[n]=t[e[n]];return r}function mt(t){return Jt.strCat("c",Jt.seq(t))}function yt(t){if(t.nUnique()!==t.values.length)throw new Error("duplicate occurrence of one or more columns "+"in selection")}function gt(t,e,r){var n,i=t.length;if(r=Ht(r)?!0:r){var o=1;for(n=0;i>n;n++){t[n]=Bt(t[n]);var u=t[n].values.length;if(1!==u)if(1===o)o=u;else if(u!==o)throw new Error("incompatible column lengths found during "+"data frame construction: "+o+", "+u)}if(1!==o)for(n=0;i>n;n++)if(1===t[n].values.length){for(var s=t[n].values[0],a=st(o),l=0;o>l;l++)a[l]=s;t[n]=at(a,t[n].dtype)}}var c=null;if(i>0)for(c=t[0].dtype,n=1;i>n;n++)if(t[n].dtype!==c){c=null;break}var h=Object.create(te);return h._cols=t,h._names=e,h._index=null,h.allDtype=c,h}function bt(t){return null===t||Ht(t)?null:Kt(t)?"number":Ut(t)?"boolean":Pt(t)?"string":Yt(t)?"date":"object"}function wt(t,e){Mt(e);for(var r=le[e],n=0;n<t.length;n++)t[n]=r(t[n]);return at(t,e)}function _t(t,e){Ht(e)&&(e="object");for(var r=null,n=0;n<t.length;n++){var i=bt(t[n]);if(null!==i&&(null===r&&(r=i),r!==i||"object"===i))return at(t,"object")}var o;if(null===r){if(r=e,"object"!==r)for(o=se[r],n=0;n<t.length;n++)t[n]=o;return at(t,r)}if("object"!==r)for(o=se[r],n=0;n<t.length;n++)ut(t[n])&&(t[n]=o);return at(t,r)}function xt(t){return ut(t)?se.number:+t}function Nt(t){return ut(t)?se["boolean"]:!!t}function Et(t){return ut(t)?se.string:"object"!=typeof t?t.toString():Yt(t)?t.toISOString():t.toString()}function Ot(t){if(ut(t))return se.date;var e=new Date(t);return Number.isNaN(e.getTime())?null:e}function jt(t){return null===t?"null":Ht(t)?"undefined":"object"!=typeof t?t.toString():Yt(t)?t.toISOString():t.toString()}function Ct(t,e){var r=ut(t),n=ut(e);if(r&&n)t=Ie[t],e=Ie[e];else{if(r)return-1;if(n)return 1}return e>t?-1:t>e?1:0}function At(t){return function(e,r){return-t(e,r)}}function zt(t,e){if(t.length!==e.length)throw new Error('length of "ascending" must match the number of '+"sort columns");if(0===t.length)return null;for(var r=t[0].size(),n=st(r),i=0;r>i;i++)n[i]=i;var o=e.map(function(t){return t?Ct:Re}),u=function(e,r){for(var n=0,i=0;i<o.length;i++){var u=t[i].values[e],s=t[i].values[r];if(n=o[i](u,s),0!==n)return n}return Ct(e,r)};return n.sort(u),n}function Mt(t){if(!(t in ue))throw new Error('invalid dtype: "'+t+'"')}function St(t,e){if(!Number.isInteger(t))throw new Error('expected an integer for "'+e+'" but got: '+t)}function kt(t,e){if(!Number.isInteger(t)||0>t)throw new Error('"'+e+'" must be a nonnegative integer')}function It(t,e){if("function"!=typeof t)throw new Error('"'+e+'" must be a function')}function Rt(t,e){var r=bt(t);if(null!==r&&r!==e)throw new Error('expected scalar to match dtype "'+e+'" but instead got '+t+', which has dtype "'+r+'"')}function Vt(t,e){if(t.dtype!==e)throw new Error('expected vector dtype to be "'+e+'" but got "'+t.dtype+'"')}function qt(t,e){if(t.dtype===e)throw new Error('unsupported operation for dtype "'+e+'"')}function Lt(t){if(t._dtypesVector().contains("object"))throw new Error("unsupported operation over data frame columns "+'with "object" dtype')}function Tt(t,e){var r=Bt(e),n=Jt.vector(Object.keys(t),"string"),i=n.s(n.isIn(r).not());if(i.size()>0)throw new Error('invalid properties found in "opts": '+i.strJoin(", "))}function Dt(t,e){var r=t;if(t!==e)if(1===t)r=e;else if(1!==e)throw new Error("incompatible array lengths: "+t+" and "+e);return r}function Ft(t){if(Ht(t)||null===t)return t;var e=1,r="a scalar";if(t.type===Gt.type?(e=t.size(),t=t.values[0],r="a vector"):Array.isArray(t)&&(e=t.length,t=t[0],r="an array"),1!==e)throw new Error("expected a single scalar value but got "+r+" of length "+e);return t}function Bt(t,e){return(ut(t)||t.type!==Gt.type)&&(t=Array.isArray(t)?_t(t.slice(),e):_t([t],e)),t}function Wt(t){return t=Bt(t,"string"),"string"!==t.dtype?t.toDtype("string"):t}function Kt(t){return"number"==typeof t}function Ut(t){return"boolean"==typeof t}function Pt(t){return"string"==typeof t}function Yt(t){return"[object Date]"===Object.prototype.toString.call(t)}function Ht(t){return"undefined"==typeof t}var Jt=t;Jt.version="0.2.0",Number.isNaN=Number.isNaN||function(t){return t!==t},Number.isInteger=Number.isInteger||function(t){return"number"==typeof t&&isFinite(t)&&Math.floor(t)===t},Array.prototype.findIndex||(Array.prototype.findIndex=function(t){if(null===this)throw new TypeError("Array.prototype.findIndex called on null or undefined");if("function"!=typeof t)throw new TypeError("predicate must be a function");for(var e,r=Object(this),n=r.length>>>0,i=arguments[1],o=0;n>o;o++)if(e=r[o],t.call(i,e,o,r))return o;return-1}),String.prototype.includes||(String.prototype.includes=function(t,e){return"number"!=typeof e&&(e=0),e+t.length>this.length?!1:this.indexOf(t,e)!==-1}),String.prototype.startsWith||(String.prototype.startsWith=function(t,e){return e=e||0,this.substr(e,t.length)===t}),String.prototype.endsWith||(String.prototype.endsWith=function(t,e){var r=this.toString();("number"!=typeof e||!isFinite(e)||Math.floor(e)!==e||e>r.length)&&(e=r.length),e-=t.length;var n=r.indexOf(t,e);return n!==-1&&n===e});var Gt={};Gt.type="jsdataframe.Vector";var Qt=Object.create(Gt),Xt=Object.create(Gt),Zt=Object.create(Gt),$t=Object.create(Gt),te={};te.type="jsdataframe.DataFrame";var ee={};ee.type="jsdataframe.Range";var re={};re.type="jsdataframe.ByDtype";var ne={};ne.type="jsdataframe.Exclusion";var ie={};ie.type="jsdataframe.AbstractIndex";var oe=Object.create(ie);oe.type="jsdataframe.NestedIndex";var ue=Object.create(null);ue.number=!0,ue["boolean"]=!0,ue.string=!0,ue.date=!0,ue.object=!0;var se={number:NaN,"boolean":null,string:null,date:null,object:null},ae={number:Qt,"boolean":Xt,string:Zt,date:$t,object:Gt},le={number:xt,"boolean":Nt,string:Et,date:Ot,object:function(t){return t}};Jt._private_export={},Jt.vector=function(t,e,r){if(!Array.isArray(t))throw new Error('"array" argument must be an Array');return(Ht(r)||r)&&(t=t.slice()),Ht(e)&&(e=null),null===e?_t(t):wt(t,e)},Jt.seq=function(t,r,n,i){if(1===arguments.length){if(!Kt(t))throw new Error('both "start" and "stop" arguments must be '+"specified for non-numeric sequences");r=t,t=0}else if(bt(t)!==bt(r))throw new Error('"start" and "stop" must have the same dtype');if(n=Ht(n)?1:n,i=Ht(i)?!1:i,Pt(t)){if(1!==t.length||1!==r.length)throw new Error('both "start" and "stop" must be single characters '+"for character sequences");var o=Jt.seq(t.charCodeAt(0),r.charCodeAt(0),n,i);return o.map(e)}if(0===n)throw new Error('"step" must be nonzero');if(r>t&&0>n)throw new Error('"step" must be positive when start < stop');if(t>r&&n>0)throw new Error('"step" must be negative when start > stop');for(var u=[],s=t;n>0?i?r>=s:r>s:i?s>=r:s>r;)u.push(s),s+=n;return at(u,"number")},Jt.seqOut=function(t,r,n){if(arguments.length<3&&(n=1),n=+n,kt(r,"lengthOut"),Pt(t)){if(1!==t.length)throw new Error('"start" must be a single character '+"for character sequences");var i=Jt.seqOut(t.charCodeAt(0),r,n);return i.map(e)}for(var o=st(r),u=t,s=0;r>s;s++)o[s]=u,u+=n;return at(o,"number")},Jt.linspace=function(t,e,r){t=+t,e=+e,kt(r,"length");for(var n=st(r),i=1===r?0:(e-t)/(r-1),o=0;r>o;o++)n[o]=t+o*i;return at(n,"number")},Jt.rep=function(t,e){kt(e,"times"),t=Bt(t);for(var r=t.values,n=r.length,i=st(n*e),o=0;e>o;o++)for(var u=o*n,s=0;n>s;s++)i[u+s]=r[s];return at(i,t.dtype)},Jt.repEach=function(t,e){kt(e,"times"),t=Bt(t);for(var r=t.values,n=r.length,i=st(n*e),o=0;n>o;o++)for(var u=o*e,s=0;e>s;s++)i[u+s]=r[o];return at(i,t.dtype)},Jt.repNa=function(t,e){kt(t,"times"),Mt(e);for(var r=se[e],n=st(t),i=0;t>i;i++)n[i]=r;return at(n,e)},Jt.df=function(t,e){Ht(e)||(e=Wt(e));var r;if(t.type===Gt.type)throw new Error('"columns" should not itself be a vector');if(Array.isArray(t)){if(r=t.length,Ht(e))e=mt(r);else if(e.size()!==r)throw new Error('the length of "colNames" ('+e.size()+') does not match the length of "columns" ('+r+")")}else{if("object"!=typeof t)throw new Error('expected "columns" to be an array or object but got: ',t);var n=Object.keys(t);r=n.length;var i=t;if(Ht(e))e=at(n,"string");else{if(e.isNa().any())throw new Error('"colNames" cannot have null entries when '+'"columns" is an object');if(e.duplicated().any())throw new Error('"colNames" cannot have duplicate entries when '+'"columns" is an object');if(e.size()!==r||e.isIn(n).not().any())throw new Error('"colNames" must match all the keys in '+'"columns" if "columns" is an object')}t=st(r);for(var o=0;r>o;o++)t[o]=i[e.values[o]]}return gt(t,e)},Jt.dfFromObjArray=function(t,e){if(!Array.isArray(t))throw new Error('"objArray" must be an array');var r,n,i=t.length,o=!0;if(Ht(e))e=[],n=[],o=!1;else{if(e=Wt(e),e.isNa().any())throw new Error('"colOrder" cannot have null entries');if(e.duplicated().any())throw new Error('"colOrder" cannot have duplicate entries');for(e=e.values,n=st(e.length),r=0;r<e.length;r++)n[r]=st(i)}for(var u=Object.create(null),s=0;i>s;s++){var a=t[s];if(!o){var l=Object.keys(a);for(r=0;r<l.length;r++){var c=l[r];if(!(c in u)){e.push(c);for(var h=st(i),f=0;s>f;f++)h[f]=null;n.push(h),u[c]=c}}}for(r=0;r<e.length;r++)n[r][s]=a.propertyIsEnumerable(e[r])?a[e[r]]:null}return gt(n,at(e,"string"))},Jt.dfFromMatrix=function(t,e){if(!Array.isArray(t))throw new Error('"matrix" must be an array');var n=t.length>0?t[0].length:0;if(e=Ht(e)?mt(n):Wt(e),n>0&&n!==e.size())throw new Error('"colNames" must have the same length as each '+"row array");return r(t,0,e)},Jt.dfFromMatrixWithHeader=function(t){if(!Array.isArray(t))throw new Error('"matrix" must be an array');if(0===t.length)throw new Error('"matrix" must not have length 0');var e=Wt(t[0]);if(t.length>1&&e.size()!==t[1].length)throw new Error("header row must have the same length as other "+"row arrays");return r(t,1,e)},Jt.unpack=function(t){if(t.type===Gt.type)return n(t);if(t.type===te.type){var e=n(t.names),r=t.cols.map(function(t){return n(t)});return Jt.df(r,e)}throw new Error('"obj" has unrecognized type: '+t.type)},Jt.vCat=function(){for(var t=arguments.length,e=st(t),r=null,n=0,i=0;t>i;i++){var o=arguments[i];if(!Ht(o)&&null!==o&&o.type===te.type)throw new Error("cannot pass data frame arguments to jd.vCat");var u=Bt(o);null===r&&"object"!==u.dtype&&(r=u.dtype),n+=u.size(),e[i]=u}r=null===r?"object":r;var s=st(n),a=0;for(i=0;t>i;i++)for(var l=e[i].values,c=l.length,h=0;c>h;h++)s[a]=l[h],a++;return _t(s,r)},Jt.colCat=function(){for(var t=arguments.length,e=st(t),r=0;t>r;r++)e[r]=arguments[r];return Jt._colCatArray(e)},Jt._colCatArray=function(t){for(var e,r=t.length,n=[],i=[],o=0;r>o;o++){var u=t[o];if(Ht(u)||null===u)n.push(u),i.push(null);else if(u.type===te.type){var s=u._cols.length;for(e=0;s>e;e++)n.push(u._cols[e]),i.push(u._names.values[e])}else if("object"!=typeof u||u.type===Gt.type||Array.isArray(u))n.push(u),i.push(null);else{var a=Object.keys(u);for(e=0;e<a.length;e++){var l=a[e];n.push(u[l]),i.push(l)}}}return gt(n,at(i,"string"))},Jt.rowCat=function(){for(var t=arguments.length,e=st(t),r=0;t>r;r++)e[r]=arguments[r];return Jt._rowCatArray(e)};var ce={SCALAR:0,ARRAY:1,VECTOR:2,DATA_FRAME:3};Jt._rowCatArray=function(t){var e,r,n,i=t.length,o=st(i),u=null,s=0,a=-1;for(r=0;i>r;r++){e=t[r];var l;if(Ht(e)||null===e||"object"!=typeof e)o[r]=ce.SCALAR,s++,l=a;else if(e.type===te.type){if(o[r]=ce.DATA_FRAME,l=e.nCol(),0===l)continue;if(s+=e.nRow(),null===u)u=e._names.values.slice();else{var c=Math.min(u.length,l),h=e._names.values;for(n=0;c>n;n++)h[n]!==u[n]&&(u[n]=null)}}else e.type===Gt.type?(o[r]=ce.VECTOR,s++,l=e.values.length):Array.isArray(e)?(o[r]=ce.ARRAY,s++,l=e.length):(o[r]=ce.SCALAR,s++,l=a);if(a===-1)a=l;else if(a!==l)throw new Error("arguments imply differing number of columns: "+a+", "+l)}if(0===s)return Jt.df([]);a===-1&&(a=1);var f=null===u?Jt.repNa(a,"string"):at(u,"string"),v=st(a);for(n=0;a>n;n++)v[n]=st(s);var p=0;for(r=0;i>r;r++)switch(e=t[r],o[r]){case ce.SCALAR:for(n=0;a>n;n++)v[n][p]=e;p++;break;case ce.VECTOR:e=e.values;case ce.ARRAY:for(n=0;a>n;n++)v[n][p]=e[n];p++;break;case ce.DATA_FRAME:var d=e.nRow();for(n=0;a>n;n++)for(var m=0;d>m;m++)v[n][p+m]=e._cols[n].values[m];p+=d}return gt(v,f)},Jt.strCat=function(){var t=arguments.length;if(0===t)throw new Error('"strCat" must be called with at least one argument');for(var e=st(t),r=0;t>r;r++)e[r]=Bt(arguments[r]).values;var n=Object.create(null),i=vt(e,he,n);return at(i,"string")};var he=U(function(){for(var t=arguments.length,e=st(t),r=0;t>r;r++){var n=arguments[r];if(ut(n))return null;e[r]=jt(n)}return e.join("")}),fe=55,ve=4,pe=45,de=6,me=6,ye=Math.pow(10,1-de),ge=1e7-1e-9,be=" ",we="..",_e=":";Jt.printingOpts={},Jt.printingOpts._maxWidth=79,Jt.printingOpts._maxLines=10,Jt.printingOpts._printCallback=function(t){console.log(t)},Jt.printingOpts.getMaxWidth=function(){return this._maxWidth},Jt.printingOpts.setMaxWidth=function(t){l(t,fe,"maxWidth"),this._maxWidth=t},Jt.printingOpts.getMaxLines=function(){return this._maxLines},Jt.printingOpts.setMaxLines=function(t){l(t,ve,"maxLines"),this._maxLines=t},Jt.printingOpts.setPrintFunction=function(t){It(t,"callback"),this._printCallback=t},Gt.p=function(t){var e=this.printToString(t);Jt.printingOpts._printCallback(e)},Gt.printToString=function(t){if(Ht(t)?t=Jt.printingOpts._maxLines:l(t,ve,"maxLines"),0===this.values.length)return this.toString();var e=u(s(this.values.length,t)),r=u(this._toTruncatedPrintVector(t)),n=Jt.strCat(e,be,r);return this.toString()+"\n"+n.strJoin("\n")},te.p=function(t){var e=this.printToString(t);Jt.printingOpts._printCallback(e)},te.printToString=function(t){Ht(t)?t=Jt.printingOpts._maxLines:l(t,ve,"maxLines");for(var e=u(Jt.vCat("",s(this.nRow(),t))),r=[e],n=0,i=e.at(0).length,o=Jt.printingOpts._maxWidth-we.length-be.length;o>=i&&n<this.nCol();){var c=this._cols[n]._toTruncatedPrintVector(t),h=u(Jt.vCat(a(this._names.at(n)),c));r.push(be),r.push(h),i+=be.length+h.at(0).length,n++}i>o&&(r.pop(),r.push(we));var f=Jt.strCat.apply(Jt,r);return this.toString()+"\n"+f.strJoin("\n")},Gt._toTruncatedPrintVector=function(t){if(this.values.length>t){var e=Math.ceil(t/2-1),r=Jt.rng(0,e),n=Jt.rng(-e),i=this.s([r,n])._toPrintVector();return Jt.vCat(i.s(r),we,i.s(n))}return this._toPrintVector()},Gt._toPrintVector=function(){return this.map(a)},Qt._toPrintVector=function(){if(this.values.some(i)||this.values.some(o))return this.map(function(t){return t.toExponential(me)});var t=Math.min(de,this.map(c).max());return this.map(function(e){return e.toFixed(t)})},Jt._private_export.toPrintString=a,Jt._private_export.fractionDigits=c,Gt.dtype="object",Gt._init=function(t){this.values=t,this._index=null},Gt.size=function(){return this.values.length},Gt.toString=function(){return"Vector[dtype:"+this.dtype+", size:"+this.values.length+"]"},Gt.toArray=function(){return this.values.slice()},Gt.toDtype=function(t){if(this.dtype===t)return this;if("string"===t){var e=Object.create(null),r=vt([this.values],xe,e);return at(r,"string")}return Jt.vector(this.values,t)};var xe=U(Et);Gt.pack=function(){return h(this)},Gt.isNa=function(){return at(this.values.map(ut),"boolean")},Gt.dropNa=function(){return this.filter(f)},Gt.replaceNa=function(t){var e=le[this.dtype];t=e(t);for(var r=this.values.slice(),n=0;n<r.length;n++)ut(r[n])&&(r[n]=t);return at(r,this.dtype)},Gt.s=function(t){var e=H(t,this.values.length);if(null===e)return this;var r=dt(this.values,e.values);return at(r,this.dtype)},Gt.sMod=function(t,e){var r=H(t,this.values.length);null===r&&(r=Jt.seq(this.values.length)),e=Bt(e,this.dtype),Vt(e,this.dtype);var n=1===e.size();if(!n&&e.size()!==r.size())throw new Error("length mismatch: cannot assign "+e.size()+" values to a selection of length "+r.size());for(var i=r.values,o=this.values.slice(),u=0;u<i.length;u++)o[i[u]]=n?e.values[0]:e.values[u];return at(o,this.dtype)},Gt.at=function(t){return t=Ft(t),t=et(t,this.size()),this.values[t]},Gt.head=function(t){return Ht(t)&&(t=6),St(t,"n"),this.s(Jt.rng(0,t))},Gt.tail=function(t){Ht(t)&&(t=6),St(t,"n");var e=0>t?-t:this.size()-t;return this.s(Jt.rng(e,void 0))},Gt.ifElse=function(t,e){if(t=Bt(t,"boolean"),t.size()!==this.size())throw new Error('"cond" must be the same length as this vector');Vt(t,"boolean"),e=Bt(e,this.dtype),Vt(e,this.dtype);var r=vt([this.values,t.values,e.values,[se[this.dtype]]],v);return at(r,this.dtype)},Gt.ex=function(){return Jt.ex(this)},Gt.map=function(){var t=Array.prototype.map.apply(this.values,arguments);return _t(t,this.dtype)},Gt.reduce=function(){return Array.prototype.reduce.apply(this.values,arguments)},Gt.reduceRight=function(){return Array.prototype.reduceRight.apply(this.values,arguments)},Gt.findIndex=function(){return Array.prototype.findIndex.apply(this.values,arguments)},Gt.indexOf=function(t){if("object"===this.dtype)return this.values.indexOf(t);var e=this._getIndex().lookupKey([t]);return null===e?-1:"number"==typeof e?e:e[0]},Gt.sort=function(t){Ht(t)&&(t=Ct);var e=this.values.slice();return Array.prototype.sort.call(e,t),at(e,this.dtype)},Gt.reverse=function(){var t=this.values.slice();return Array.prototype.reverse.call(t),at(t,this.dtype)},Gt.filter=function(){var t=Array.prototype.filter.apply(this.values,arguments);return at(t,this.dtype)},Gt.strJoin=function(t){return this.values.map(jt).join(t)},Gt.combine=function(){var t=arguments.length;if(2>t)throw new Error('must supply at least one "varg"');var e=arguments[t-1];if("function"!=typeof e)throw new Error('the last argument to "combine" must be a function');var r=st(t);r[0]=this.values;for(var n=1;t>n;n++)r[n]=Bt(arguments[n-1]).values;var i=vt(r,e);return Jt.vector(i)},Gt.eq=function(t){t=Bt(t,this.dtype);var e=Dt(this.size(),t.size());if(this.dtype!==t.dtype)return Jt.repNa(e,"boolean");var r="object"===this.dtype?ft(this.values,t.values,null,d):ft(this.values,t.values,null,p);return at(r,"boolean")},Gt.neq=function(t){return this.eq(t).not()},Gt.lt=function(t){t=Bt(t,this.dtype);var e=Dt(this.size(),t.size());if(this.dtype!==t.dtype)return Jt.repNa(e,"boolean");var r=ft(this.values,t.values,null,m);return at(r,"boolean")},Gt.gt=function(t){t=Bt(t,this.dtype);var e=Dt(this.size(),t.size());if(this.dtype!==t.dtype)return Jt.repNa(e,"boolean");var r=ft(this.values,t.values,null,y);return at(r,"boolean")},Gt.lte=function(t){return this.lt(t).or(this.eq(t))},Gt.gte=function(t){return this.gt(t).or(this.eq(t))},Gt.between=function(t,e,r){throw new Error("unimplemented method (TODO)")},Gt.equals=function(t,e){if(ut(t)||t.type!==Gt.type||this.size()!==t.size()||this.dtype!==t.dtype)return!1;if(this===t)return!0;var r=p;"number"===this.dtype?r=Ht(e)?g:function(t,r){return Number.isNaN(t)&&Number.isNaN(r)||Math.abs(t-r)<=e}:"object"===this.dtype&&(r=d);for(var n=this.values,i=t.values,o=0;o<n.length;o++)if(!r(n[o],i[o]))return!1;return!0},Gt.min=function(t){var e=this.idxMin(t);return Number.isNaN(e)?se[this.dtype]:this.values[e]},Gt.max=function(t){var e=this.idxMax(t);return Number.isNaN(e)?se[this.dtype]:this.values[e]},Gt.cuMin=function(t){Ht(t)&&(t=!0);var e=t?pt(this.values,b):pt(this.values,se[this.dtype],b);return at(e,this.dtype)},Gt.cuMax=function(t){Ht(t)&&(t=!0);var e=t?pt(this.values,w):pt(this.values,se[this.dtype],w);return at(e,this.dtype)},Gt.idxMin=function(t){Ht(t)&&(t=!0);for(var e=this.values,r=NaN,n=null,i=0;i<e.length;i++){var o=e[i];if(ut(o)){if(!t)return NaN}else(Ct(o,n)<0||Number.isNaN(r))&&(n=o,r=i)}return r},Gt.idxMax=function(t){Ht(t)&&(t=!0);for(var e=this.values,r=NaN,n=null,i=0;i<e.length;i++){var o=e[i];if(ut(o)){if(!t)return NaN}else(Ct(o,n)>0||Number.isNaN(r))&&(n=o,r=i)}return r},Gt.pMin=function(t){t=Bt(t,this.dtype),Vt(t,this.dtype);var e=ft(this.values,t.values,se[this.dtype],b);return at(e,this.dtype)},Gt.pMax=function(t){t=Bt(t,this.dtype),Vt(t,this.dtype);var e=ft(this.values,t.values,se[this.dtype],w);return at(e,this.dtype)},Gt.clip=function(t,e){t=Bt(t,this.dtype),e=Bt(e,this.dtype),Vt(t,this.dtype),Vt(e,this.dtype);var r=vt([this.values,t.values,e.values],_);return at(r,this.dtype)},Gt.rank=function(){throw new Error("unimplemented method (TODO)")},Gt.contains=function(t){qt(this,"object"),t=Ft(t);var e=bt(t);if(e!==this.dtype&&null!==e)throw new Error('"value" does not match the dtype of this vector');return null!==this._getIndex().lookupKey([t])},Gt.isIn=function(t){return qt(this,"object"),t=Bt(t,this.dtype),Vt(t,this.dtype),t._getIndex().has([this])},Gt.valueCounts=function(){qt(this,"object");var t=this._getIndex().valueCounts(),e=Jt.df([t.vectors[0],t.counts],["value","count"]);return e.sort(["count","value"],[!1,!0])},Gt.unique=function(){return qt(this,"object"),this._getIndex().unique()[0]},Gt.nUnique=function(){return qt(this,"object"),this._getIndex().size},Gt.duplicated=function(t){return qt(this,"object"),this._getIndex().duplicated(t)},Gt.replace=function(){throw new Error("unimplemented method (TODO)")},Gt.describe=function(){throw new Error("unimplemented method (TODO)")},Gt._getIndex=function(){return null===this._index&&(this._index=ot([this])),this._index},Gt.union=function(t){return qt(this,"object"),t=Bt(t,this.dtype),Vt(t,this.dtype),Jt.vCat(this,t).unique()},Gt.intersect=function(t){qt(this,"object"),t=Bt(t,this.dtype),Vt(t,this.dtype);var e=this.unique();return e.s(e.isIn(t))},Gt.setdiff=function(t){qt(this,"object"),t=Bt(t,this.dtype),Vt(t,this.dtype);var e=this.unique();return e.s(e.isIn(t).ex())},Qt.dtype="number",Qt.add=function(t){t=Bt(t,"number"),Vt(t,"number");var e=ft(this.values,t.values,NaN,x);return at(e,"number")},Qt.sub=function(t){t=Bt(t,"number"),Vt(t,"number");var e=ft(this.values,t.values,NaN,N);return at(e,"number")},Qt.mul=function(t){t=Bt(t,"number"),Vt(t,"number");var e=ft(this.values,t.values,NaN,E);return at(e,"number")},Qt.div=function(t){t=Bt(t,"number"),Vt(t,"number");var e=ft(this.values,t.values,NaN,O);return at(e,"number")},Qt.mod=function(t){t=Bt(t,"number"),Vt(t,"number");var e=ft(this.values,t.values,NaN,j);return at(e,"number")},Qt.pow=function(t){t=Bt(t,"number"),Vt(t,"number");var e=ft(this.values,t.values,NaN,Math.pow);return at(e,"number")},Qt.abs=function(){return at(this.values.map(Math.abs),"number")},Qt.sqrt=function(){return at(this.values.map(Math.sqrt),"number")},Qt.sign=function(){return at(this.values.map(Math.sign),"number")},Qt.ceil=function(){return at(this.values.map(Math.ceil),"number")},Qt.floor=function(){return at(this.values.map(Math.floor),"number")},Qt.round=function(){return at(this.values.map(Math.round),"number")},Qt.exp=function(){return at(this.values.map(Math.exp),"number")},Qt.log=function(){return at(this.values.map(Math.log),"number")},Qt.sin=function(){return at(this.values.map(Math.sin),"number")},Qt.cos=function(){return at(this.values.map(Math.cos),"number");
},Qt.tan=function(){return at(this.values.map(Math.tan),"number")},Qt.asin=function(){return at(this.values.map(Math.asin),"number")},Qt.acos=function(){return at(this.values.map(Math.acos),"number")},Qt.atan=function(){return at(this.values.map(Math.atan),"number")},Qt.sum=function(t){return Ht(t)&&(t=!0),t?ct(this.values,0,x):ht(this.values,0,ut,x)},Qt.cuSum=function(t){Ht(t)&&(t=!0);var e=t?pt(this.values,x):pt(this.values,se.number,x);return at(e,"number")},Qt.mean=function(t){Ht(t)&&(t=!0);var e={n:0,sum:0},r=t?ct(this.values,e,C):ht(this.values,e,ut,C);return Number.isNaN(r)||0===r.n?NaN:r.sum/r.n},Qt.stdev=function(t){var e=this["var"](t);return Number.isNaN(e)?NaN:Math.sqrt(e)},Qt["var"]=function(t){Ht(t)&&(t=!0);var e={n:0,mean:0,M2:0},r=t?ct(this.values,e,A):ht(this.values,e,ut,A);return Number.isNaN(r)||r.n<2?NaN:r.M2/(r.n-1)},Xt.dtype="boolean",Xt.and=function(t){t=Bt(t,"boolean"),Vt(t,"boolean");var e=ft(this.values,t.values,z);return at(e,"boolean")},Xt.or=function(t){t=Bt(t,"boolean"),Vt(t,"boolean");var e=ft(this.values,t.values,M);return at(e,"boolean")},Xt.not=function(){var t=lt(this.values,null,S);return at(t,"boolean")},Xt.xor=function(t){t=Bt(t,"boolean"),Vt(t,"boolean");var e=ft(this.values,t.values,k);return at(e,"boolean")},Xt.all=function(t){return Ht(t)&&(t=!1),t?ct(this.values,!0,z):this.values.reduce(z,!0)},Xt.any=function(t){return Ht(t)&&(t=!1),t?ct(this.values,!1,M):this.values.reduce(M,!1)},Xt.sum=function(t){if(Ht(t)&&(t=!0),t)return ct(this.values,0,I);var e=ht(this.values,0,ut,I);return null===e?NaN:e},Xt.which=function(){for(var t=[],e=0;e<this.values.length;e++){var r=this.values[e];r===!0&&t.push(e)}return at(t,"number")},Zt.dtype="string",Zt.nChar=function(){var t=lt(this.values,se.number,R);return at(t,"number")},Zt.charAt=function(t){t=Bt(t);var e=Object.create(null),r=vt([this.values,t.values],Ne,e);return at(r,"string")};var Ne=U(function(t,e){return null===t?null:t.charAt(e)});Zt.trim=function(){var t=Object.create(null),e=vt([this.values],Ee,t);return at(e,"string")};var Ee=U(function(t){return null===t?null:t.trim()});Zt.strSlice=function(t,e){t=Bt(t),e=Bt(e);var r=Object.create(null),n=vt([this.values,t.values,e.values],Oe,r);return at(n,"string")};var Oe=U(function(t,e,r){return null===t?null:t.slice(e,r)});Zt.substr=function(t,e){t=Bt(t),e=Bt(e);var r=Object.create(null),n=vt([this.values,t.values,e.values],je,r);return at(n,"string")};var je=U(function(t,e,r){return null===t?null:t.substr(e,r)});Zt.strIncludes=function(t,e){t=Bt(t),e=Bt(e);var r=vt([this.values,t.values,e.values],V);return at(r,"boolean")},Zt.startsWith=function(t,e){t=Bt(t),e=Bt(e);var r=vt([this.values,t.values,e.values],q);return at(r,"boolean")},Zt.endsWith=function(t,e){t=Bt(t),e=Bt(e);var r=vt([this.values,t.values,e.values],L);return at(r,"boolean")},Zt.strIndexOf=function(t,e){t=Bt(t),e=Bt(e);var r=vt([this.values,t.values,e.values],T);return at(r,"number")},Zt.strLastIndexOf=function(t,e){t=Bt(t),e=Bt(e);var r=vt([this.values,t.values,e.values],D);return at(r,"number")},Zt.regexMatch=function(t){t=Bt(t);var e=Object.create(null),r=vt([this.values,t.values],Ce,e);return at(r,"string")};var Ce=U(function(t,e){if(null===t)return null;var r=t.match(e);return null===r?null:r[0]});Zt.regexSearch=function(t){t=Bt(t);var e=vt([this.values,t.values],F);return at(e,"number")},Zt.regexTest=function(t){t=Bt(t);var e=vt([this.values,t.values],B);return at(e,"boolean")},Zt.strReplace=function(t,e){t=Bt(t),e=Bt(e);var r=Object.create(null),n=vt([this.values,t.values,e.values],Ae,r);return at(n,"string")};var Ae=U(function(t,e,r){return null===t?null:t.replace(e,r)});Zt.toLowerCase=function(){var t=Object.create(null),e=vt([this.values],W,t);return at(e,"string")},Zt.toUpperCase=function(){var t=Object.create(null),e=vt([this.values],K,t);return at(e,"string")},$t.dtype="date",te.nRow=function(){return 0===this._cols.length?0:this._cols[0].values.length},te.nCol=function(){return this._cols.length},te.dtypes=function(){return Jt.df([this.names(),this._dtypesVector()],["colName","dtype"])},te._dtypesVector=function(){return at(this._cols.map(function(t){return t.dtype}),"string")},te.colArray=function(){return this._cols.slice()},te.colMap=function(){var t=this._names.dropNa().unique().values,e=t.map(function(t){return nt(t,this._names)},this),r=Object.create(null);return e.forEach(function(e,n){var i=t[n];r[i]=this._cols[e]},this),r},te.equals=function(t,e){if(null===t||Ht(t)||t.type!==te.type||!t.names().equals(this.names()))return!1;for(var r=this.nCol(),n=0;r>n;n++)if(!t._cols[n].equals(this._cols[n],e))return!1;return!0},te.toString=function(){var t="DataFrame[nRow:"+this.nRow()+", nCol:"+this.nCol()+", allDtype:"+this.allDtype+"]";return t},te.toObjArray=function(){for(var t=this._names.dropNa().unique().values,e=t.map(function(t){return nt(t,this._names)},this),r=this.nRow(),n=t.length,i=st(r),o=0;r>o;o++){for(var u={},s=0;n>s;s++)u[t[s]]=this._cols[e[s]].values[o];i[o]=u}return i},te.toMatrix=function(t){t=Ht(t)?!1:t;var e=t?1:0,r=this.nRow(),n=st(r+e);t&&(n[0]=this._names.values);for(var i=this.nCol(),o=0;r>o;o++){for(var u=st(i),s=0;i>s;s++)u[s]=this._cols[s].values[o];n[o+e]=u}return n},te.pack=function(){var t={type:te.type,version:Jt.version};return t.names=h(this.names(),!1),t.cols=this._cols.map(function(t){return h(t,!1)}),t},te.names=function(){return this._names},te.setNames=function(t){if(t=Wt(t),t.size()!==this._cols.length)throw new Error('the length of "names" must match the number of columns');return gt(this._cols,t,!1)},te.rename=function(t){throw new Error("unimplemented method (TODO)")},te.resetNames=function(){return gt(this._cols,mt(this._cols.length),!1)},te.isNa=function(){return this.mapCols(function(t){return t.isNa()})},te.dropNa=function(){var t=this.nCol(),e=this.nRow();if(0===e)return this;for(var r=st(e),n=0;e>n;n++){r[n]=!0;for(var i=0;t>i;i++)if(ut(this._cols[i].values[n])){r[n]=!1;break}}return this.s(r)},te.s=function(t,e){var r=H(t,this.nRow()),n=J(e,this._names,this._dtypesVector()),i=this._names.values,o=this._cols;null!==n&&(i=dt(i,n.values),o=dt(o,n.values));var u=at(i,"string");if(null!==r){null===n&&(o=o.slice());for(var s=0;s<o.length;s++){var a=o[s].dtype,l=dt(o[s].values,r.values);o[s]=at(l,a)}}return gt(o,u,!1)},te.sMod=function(t,e,r){var n=!Ht(r)&&null!==r&&r.type===te.type;n||(r=Ft(r));var i=H(t,this.nRow()),o=J(e,this._names,this._dtypesVector());if(null===o?o=Jt.seq(this.nCol()):yt(o),n){var u=null===i?this.nRow():i.size();if(u!==r.nRow()||o.size()!==r.nCol())throw new Error('"values" has the wrong dimensions for selection')}for(var s=this._cols.slice(),a=0;a<o.values.length;a++){var l=o.values[a],c=n?r._cols[a]:r;s[l]=null!==i?s[l].sMod(i,c):n?c:Jt.rep(c,this.nRow())}return gt(s,this.names(),!1)},te.c=function(t){var e=nt(t,this._names);return this._cols[e]},te.cMod=function(t,e){var r=this._cols.slice(),n=nt(t,this._names);return r[n]=e,gt(r,this._names)},te.insertCol=function(t,e,r){if(t=Et(t),r=Ht(r)?this._cols.length:r,r=Ft(r),!Number.isInteger(r))throw new Error('expected "index" to be an integer but got: '+r);var n=this._cols.slice(),i=this._names.values.slice();return n.splice(r,0,e),i.splice(r,0,t),gt(n,at(i,"string"))},te.at=function(t,e){if(t=Ft(t),t=et(t,this.nRow()),e=Ft(e),Kt(e))e=et(e,this.nCol());else{if(!Pt(e)&&null!==e)throw new Error('expected "j" to be an integer or string but got: '+e);e=nt(e,this._names)}return this._cols[e].values[t]},te.locAt=function(t,e,r){var n=this.c(t),i=nt(r,this._names),o=n.indexOf(e);if(o===-1)throw new Error("no match for lookup key: "+e);return this._cols[i].values[o]},te.head=function(t){return Ht(t)&&(t=6),St(t,"n"),this.s(Jt.rng(0,t))},te.tail=function(t){Ht(t)&&(t=6),St(t,"n");var e=0>t?-t:this.nRow()-t;return this.s(Jt.rng(e,void 0))},te.mapCols=function(t,e){arguments.length<2&&(e=t,t=null),It(e,"func");var r=J(t,this._names,this._dtypesVector());null===r&&(r=Jt.seq(this._cols.length));for(var n=this._names.values,i=r.values.length,o=st(i),u=st(i),s=0;i>s;s++){var a=r.values[s],l=this._cols[a],c=n[a];u[s]=e(l,c,a,s),o[s]=c}var h=at(o,"string");return gt(u,h)},te.updateCols=function(t,e){arguments.length<2&&(e=t,t=null),It(e,"func");var r=J(t,this._names,this._dtypesVector());null===r?r=Jt.seq(this._cols.length):yt(r);for(var n=this._names.values,i=this._cols.slice(),o=r.values.length,u=0;o>u;u++){var s=r.values[u],a=this._cols[s],l=n[s];i[s]=e(a,l,s,u)}return gt(i,this._names)},te.mapRowObjects=function(t,e){return It(t,"func"),this.toObjArray().map(t,e)},te.mapRowArrays=function(t,e){return It(t,"func"),this.toMatrix().map(t,e)},te.mapRowVectors=function(t,e){It(t,"func");var r=this.allDtype;if(null===r)throw new Error('cannot call "mapRowVectors" on a data frame with '+'null "allDtype" property');for(var n=this.toMatrix(),i=0;i<n.length;i++)n[i]=at(n[i],r);return n.map(t,e)},te.mapRowDfs=function(t,e){It(t,"func");for(var r=this.nRow(),n=st(r),i=0;r>i;i++)n[i]=this.s(i);return n.map(t,e)},te.unique=function(){if(0===this.nCol())return this;Lt(this);var t=this._getIndex().unique();return gt(t,this._names,!1)},te.nUnique=function(){return 0===this.nCol()?0:(Lt(this),this._getIndex().size)},te.duplicated=function(t){return 0===this.nCol()?at([],"boolean"):(Lt(this),this._getIndex().duplicated(t))},te._getIndex=function(){return 1===this._cols.length?this._cols[0]._getIndex():(null===this._index&&(this._index=ot(this._cols)),this._index)},te.groupApply=function(t,e,r){if(Ht(t))throw new Error('"colSelect" must not be undefined');var n=J(t,this._names,this._dtypesVector());if(yt(n),null===n||n.size()===this.nCol())throw new Error('"colSelect" must not select every column in the '+"data frame");if(0===n.size())throw new Error('"colSelect" must select at least 1 column');var i=this.s(null,n.ex()),o=this.s(null,n);Lt(o);var u=o._getIndex(),s=o.unique();It(e,"func"),Ht(r)||(r=Bt(r,"string"),"string"!==r.dtype&&(r=r.toDtype("string")));for(var a=s.nRow(),l=st(a),c=-1,h=0,f=0;a>f;f++){var v=s.s(f),p=u.lookup(s._cols,f),d=i.s(p),m=e(d,v);if(!Ht(m)){var y,g;if(null===m?(y=1,g=[v,m]):m.type===te.type?(y=m.nCol(),g=[v,m]):m.type===Gt.type?(y=m.size(),g=[v].concat(m.values)):Array.isArray(m)?(y=m.length,g=[v].concat(m)):(y=1,g=[v,m]),c===-1){if(c=y,!Ht(r)&&y!==r.size())throw new Error('length of "colNames" must match implied number '+'of columns returned by "func"')}else if(y!==c)throw new Error("inconsistent implied column counts returned from "+'"func": '+c+", ",+y);l[h]=Jt._colCatArray(g),h++}}if(0===h)return s.s([]);l.length!==h&&(l.length=h);var b=Jt._rowCatArray(l);if(!Ht(r)){var w=b.names();w=w.sMod(Jt.rng(n.size()),r),b=b.setNames(w)}return b},te.transpose=function(t,e){if(0===this.nRow())return Jt.df([]);if(t=Ht(t)?null:t,null!==t&&!Pt(t))throw new Error('"preservedColName" must be a string when not null or '+"undefined");var r,n,i=this.nRow();if(Ht(e))r=this,n=mt(i);else{var o=nt(e,this._names);r=this.s(null,Jt.ex(o)),n=this.c(o),"string"!==n.dtype&&(n=n.toDtype("string"))}var u,s;null!==t?(n=Jt.vCat(t,n),u=1,s=st(i+1),s[0]=r._names):(u=0,s=st(i));for(var a=r.nCol(),l=0;i>l;l++){for(var c=st(a),h=0;a>h;h++)c[h]=r._cols[h].values[l];s[l+u]=c}return gt(s,n)},te.sort=function(t,e){if(Ht(t))throw new Error('"colSelect" must not be undefined');var r=J(t,this._names,this._dtypesVector());null===r?r=Jt.seq(this.nCol()):yt(r);var n=this.s(null,r);if(Lt(n),e=Ht(e)?!0:e,e=Bt(e,"boolean"),Vt(e,"boolean"),e.isNa().any())throw new Error('"ascending" must not contain missing values');1===e.size()&&(e=Jt.rep(e,n.nCol()));var i=zt(n._cols,e.values);return this.s(i)},te.melt=function(t,e,r){e=Ht(e)||null===e?"variable":Ft(e).toString(),r=Ht(r)||null===r?"value":Ft(r).toString();var n=J(t,this._names,this._dtypesVector()),i=Jt.seq(this.nCol()).s(Jt.ex(n));if(0===i.size())throw new Error('"idVars" must not select all columns in the data frame');yt(n);var o=this.mapCols(n,function(t){return Jt.rep(t,i.size())}),u=Jt.repEach(this.names().s(i),this.nRow()),s=Jt.vCat.apply(Jt,this.s(null,i).colArray());return Jt.colCat(o,Jt.df([u,s],[e,r]))},te.pivot=function(t,e,r){r=Ht(r)?{}:r,Tt(r,["idVars","aggFunc","fillValue"]);var n=r.aggFunc,i=r.fillValue,o=nt(t,this._names),u=nt(e,this._names);if(o===u)throw new Error('"pivotCol" and "valueCol" must be different');var s;if(Ht(r.idVars)?s=Jt.seq(this.nCol()).s(Jt.ex([o,u])):(s=J(r.idVars,this._names,this._dtypesVector()),null===s&&(s=Jt.seq(this.nCol())),yt(s)),s.contains(o)||s.contains(u))throw new Error('"idVars" must not include pivotCol or valueCol');if(0===s.size())throw new Error("one or more columns must be selected as id columns");var a=Jt.df([this.c(o).unique().sort()],["variable"]),l=Jt.vCat(s,o,u),c=this.s(null,l),h=c.groupApply(Jt.rng(0,-2),function(t){t=t.setNames(["variable","value"]),Ht(n)||(t=t.groupApply("variable",function(t){return n(t.c(0))},"value"));var e=a.join(t,"left");if(e.nRow()!==a.nRow())throw new Error('must provide "aggFunc" when aggregation is required');return Ht(i)?e.c("value"):e.c("value").replaceNa(i)},a.c("variable"));return h};var ze=Jt.vector(["by","leftBy","rightBy","leftSuffix","rightSuffix","indicator"]);te.join=function(t,e,r){if(Ht(t)||null===t||t.type!==te.type)throw new Error('"other" must be a data frame');var n=!1,i="_x",o="_y",u=!1,s=!1;switch(e){case"inner":break;case"left":u=!0;break;case"right":s=!0;break;case"outer":u=!0,s=!0;break;default:throw new Error('"how" must be one of the following strings: '+'"inner", "left", "right", "outer"')}Ht(r)||(Tt(r,ze),Ht(r.indicator)||(n=r.indicator),Ht(r.leftSuffix)||(i=r.leftSuffix),Ht(r.rightSuffix)||(o=r.rightSuffix));var a=Y(this,t,r),l=a.left,c=a.right,h=this.s(null,l),f=this.s(null,l.ex()),v=t.s(null,c),p=t.s(null,c.ex());if(h.dtypes().c("dtype").contains("object")||v.dtypes().c("dtype").contains("object"))throw new Error('key columns must not have "object" dtype');if(!h.dtypes().c("dtype").equals(v.dtypes().c("dtype")))throw new Error("key columns must have matching dtypes on left "+"and right");var d,m,y=f.names().s(f.names().isIn(p.names())),g=f.names().map(function(t){return y.contains(t)?t+i:t}),b=p.names().map(function(t){return y.contains(t)?t+o:t}),w=Jt.vCat(h.names(),g,b,n?["_join"]:[]),_=v._getIndex(),x=h.nRow(),N=st(w.size());for(m=0;m<N.length;m++)N[m]=[];if(s)for(d=st(v.nRow()),m=0;m<d.length;m++)d[m]=!0;for(m=0;x>m;m++){var E=_.lookup(h._cols,m);if(null===E)u&&P(N,h,f,p,m,null,"leftOnly");else if("number"==typeof E)P(N,h,f,p,m,E,"both"),s&&(d[E]=!1);else for(var O=0;O<E.length;O++){var j=E[O];P(N,h,f,p,m,j,"both"),s&&(d[j]=!1)}}if(s){var C=v.s(d),A=p.s(d);for(x=C.nRow(),m=0;x>m;m++)P(N,C,f,A,null,m,"rightOnly",!0)}return gt(N,w)},Jt.rng=function(t,e,r){if(r=Ht(r)?null:r,null!==r&&"boolean"!=typeof r)throw new Error('"includeStop" must be either true, false, or null');var n=Kt(t)&&Pt(e)||Pt(t)&&Kt(e);if(n&&null===r)throw new Error('"includeStop" must be set to either true or false '+"when mixing numbers and strings for start/stop");var i=Object.create(ee);return i._start=t,i._stop=e,i._includeStop=r,i},ee.ex=function(){return Jt.ex(this)};var Me=Jt.vector(Object.keys(ue));Jt.byDtype=function(t){t=Bt(t),Vt(t,"string"),t=t.unique();var e=t.isIn(Me);if(!e.all()){var r=t.s(e.not()).at(0);throw new Error('invalid dtype: ""'+r+'"')}var n=Object.create(re);return n._dtypes=t,n},re.ex=function(){return Jt.ex(this)},Jt.ex=function(t){if(Ht(t))throw new Error('"selector" must not be undefined');var e=Object.create(ne);return e._selector=t,e};var Se={INT:"INT",COL:"COL",KEY:"KEY"};Gt._resolveSelectorHelper=function(t,e){var r,n,i="number"===this.dtype,o="string"===this.dtype;switch(t.resolveMode){case Se.INT:if(!i)throw new Error("expected number vector for integer indexing "+'but got vector with dtype: "'+this.dtype+'"');for(r=0;r<this.values.length;r++)e.push(et(this.values[r],t.maxLen));break;case Se.COL:if(i)for(r=0;r<this.values.length;r++)e.push(et(this.values[r],t.maxLen));else{if(!o)throw new Error("expected number or string vector for column "+'indexing but got vector with dtype: "'+this.dtype+'"');for(r=0;r<this.values.length;r++){var u=this.values[r];n=t.index.lookupKey([u]),rt(n,u,t,e)}}break;case Se.KEY:if(1!==t.index.arity)throw new Error("unimplemented case (TODO)");var s=t.index.initVectors[0].dtype;if(this.dtype!==s)throw new Error('expected "'+s+'" vector for key '+'lookup but got vector with dtype: "'+this.dtype+'"');var a=[this];for(r=0;r<this.values.length;r++)n=t.index.lookup(a,r),rt(n,this.values[r],t,e);break;default:throw new Error("Unrecognized RESOLVE_MODE: "+t.resolveMode)}},ee._resolveSelectorHelper=function(t,e){var r=tt(this._start,t,!1);if(!(r>=t.maxLen)){var n=tt(this._stop,t,!0,this._includeStop);if(!(0>=n)){r=0>r?0:r,n=n>t.maxLen?t.maxLen:n;for(var i=r;n>i;i++)e.push(i)}}},re._resolveSelectorHelper=function(t,e){if(null===t.dtypes)throw new Error("ByDtype selector can only be used for column indexing");for(var r=t.dtypes.isIn(this._dtypes).which(),n=0;n<r.values.length;n++)e.push(r.values[n])},ne._resolveSelectorHelper=function(t,e){var r=[];$(this._selector,t,r);for(var n=Q(at(r,"number"),t.maxLen),i=0;i<n.values.length;i++)e.push(n.values[i])},ie.lookup=function(t,e){throw new Error('abstract "lookup" method called without concrete '+"implementation")},ie.has=function(t){for(var e=t[0].size(),r=st(e),n=0;e>n;n++)r[n]=null!==this.lookup(t,n);return at(r,"boolean")},ie.valueCounts=function(){for(var t=this.initVectors,e=this.arity,r=this.size,n=st(e),i=0;e>i;i++)n[i]=st(r);for(var o=st(r),u=t[0].size(),s=0,a=0;u>a;a++){var l=this.lookup(t,a);if(l===a||l[0]===a){for(i=0;e>i;i++)n[i][s]=t[i].values[a];o[s]=l===a?1:l.length,s++}}var c=n.map(function(e,r){return at(e,t[r].dtype)});return{vectors:c,counts:at(o,"number")}},ie.unique=function(){return this.valueCounts().vectors},ie.duplicated=function(t){Ht(t)&&(t="first");var e=!1,r=!1;if("first"===t)e=!0;else if("last"===t)r=!0;else if(t!==!1)throw new Error('"keep" must be either "first", "last", or fal