Commit 45631d81 authored by hazrmard's avatar hazrmard
Browse files

preventing errors when converting empty index to datetime, converting numbers/strings to boolean

parent a1dc9ca6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ Python package/script for downloading trends from Building Logix Data Exchange (
## Installation

```
pip install git+https://git.isis.vanderbilt.edu/SmartBuildings/bdx@v0.2.3
pip install git+https://git.isis.vanderbilt.edu/SmartBuildings/bdx@v0.2.5
```

## Usage
+18 −2
Original line number Diff line number Diff line
@@ -17,6 +17,13 @@ import requests



BOOL_MAP = {
    'YES': True, 'Yes': True, 'yes': True, 'TRUE': True, 'True': True, 'true': True, 1: True,
    'NO': False, 'No': False, 'no': False, 'FALSE': False, 'False': False, 'false': False, 0: False
}



def get_trend(trend_id: str, username: str, password: str, start: datetime,
    end: datetime, aggregation: str='Point') -> pd.DataFrame:
    """
@@ -86,7 +93,8 @@ def get_trend(trend_id: str, username: str, password: str, start: datetime,



def parse_trend_dict(trend_dict: List[Dict[str, Any]]) -> pd.DataFrame:
def parse_trend_dict(trend_dict: List[Dict[str, Any]],
    bool_map: Dict[Any, bool]=BOOL_MAP) -> pd.DataFrame:
    # trend_dict is a list of dictionaries. Each dictionary has the structure:
    # 'propertyName': str, short property name e.g vfdPower
    # 'label': str, qualified property name e.g. 'Cell_1bFan vfdPower'
@@ -118,10 +126,17 @@ def parse_trend_dict(trend_dict: List[Dict[str, Any]]) -> pd.DataFrame:
                          .format(time_series['label']))
            series_df = pd.DataFrame({'time': []})
        series_df.set_index('time', inplace=True)
        # Try to convert boolean fields into boolean types
        if 'boolValue' in series_df:
            try:
                series_df['boolValue'] = series_df['boolValue'].map(bool_map).astype('bool')
            except Exception as e:
                warnings.warn(('Failed to convert a boolean field "{}" to boolean '
                               'type. Leaving as-is.'.format(time_series['label'])))
        # If dataframe does not have 'realValue', 'boolValue' column, it means that the
        # whole series was missing values (valueType='NULL'). So a placeholder
        # column is created with NaN values. These can then be handled on
        # per-column basis in the final DataFrame:
        # per-column basis in the final DataFrame
        if ('realValue' not in series_df) and ('boolValue' not in series_df):
            series_df['nullValue'] = np.nan
        series_df.rename(columns={'realValue': time_series['label'],
@@ -130,5 +145,6 @@ def parse_trend_dict(trend_dict: List[Dict[str, Any]]) -> pd.DataFrame:
        series_df.drop(columns='valueType', inplace=True, errors='ignore')
        frames.append(series_df)
    df = pd.concat(frames, axis='columns', join='outer', sort=True)
    if len(df) > 0:
        df.index = pd.to_datetime(df.index)
    return df
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ from distutils.core import setup


setup(name='bdx',
      version='0.2.4',
      version='0.2.5',
      description='Download trends from Building Logix data exchange (BDX)',
      author='Ibrahim Ahmed',
      author_email='ibrahim.ahmed@vanderbilt.edu',