MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.25k stars 21.42k forks source link

Example code for anomaly-detector client library gives an error #59097

Closed KinoriSR closed 4 years ago

KinoriSR commented 4 years ago

I tried running the example code to learn how the API works, but I get this error when I run it: Unable to serialize value: Timestamp('2018-03-01 00:00:00+0000', tz='UTC') as type: 'iso-8601'., TypeError: an integer is required

I tried messing with the timestamps and looking through docs, but only succeeded in more errors. Am I missing something/making a mistake?

The code is from the example on: https://docs.microsoft.com/en-us/azure/cognitive-services/anomaly-detector/quickstarts/client-libraries?tabs=windows&pivots=programming-language-python

anomaly_detector_client = AnomalyDetectorClient(ANOMALY_DETECTOR_ENDPOINT, ANOMALY_DETECTOR_KEY)

TIME_SERIES_DATA_PATH='request-data.csv' # downloaded from the example's data
series = []
data_file = pd.read_csv(TIME_SERIES_DATA_PATH, header=None, encoding='utf-8', parse_dates=[0])
for index, row in data_file.iterrows():
    series.append(Point(timestamp=row[0], value=row[1]))

request = Request(series=series, granularity=Granularity.daily)

print('Detecting anomalies in the entire time series.')

try:
    response = anomaly_detector_client.entire_detect(request)
except Exception as e:
    if isinstance(e, APIErrorException):
        print('Error code: {}'.format(e.error.code),
            'Error message: {}'.format(e.error.message))
    else:
        print(e)

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

CHEEKATLAPRADEEP-MSFT-zz commented 4 years ago

@KinoriSR, Thanks for the question! We are investigating and will update you shortly.

saikrishna-2017 commented 4 years ago

rewriting the following code data_file = pd.read_csv(TIME_SERIES_DATA_PATH, header=None, encoding='utf-8', parse_dates=[0]) to data_file = pd.read_csv(TIME_SERIES_DATA_PATH, header=None, encoding='utf-8', date_parser=[0]) resolved the issue, not sure this is expected behavior or not

parse_dates is appending "Timestamp('2018-03-01 00:00:00+0000', tz='UTC')" where as date_parser is appending "'2018-04-08T00:00:00Z'" to series[]

KinoriSR commented 4 years ago

@saikrishna-2017 I just tried this change and it complains about the str. I tried this date format earlier by passing the string myself too. The error it gives: 'str' object has no attribute 'signed_session'

saikrishna-2017 commented 4 years ago

@saikrishna-2017 I just tried this change and it complains about the str. I tried this date format earlier by passing the string myself too. The error it gives: 'str' object has no attribute 'signed_session'

Can you send full error message?

KinoriSR commented 4 years ago

Here is the full traceback when I remove the try/except:

Traceback (most recent call last):
  File "anomaly-detection-test.py", line 71, in <module>
    response = anomaly_detector_client.entire_detect(request)
  File "/usr/lib/python3/dist-packages/azure/cognitiveservices/anomalydetector/anomaly_detector_client.py", line 121, in entire_detect
    response = self._client.send(request, stream=False, **operation_config)
  File "/home/kinori/.local/lib/python3.8/site-packages/msrest/service_client.py", line 336, in send
    pipeline_response = self.config.pipeline.run(request, **kwargs)
  File "/home/kinori/.local/lib/python3.8/site-packages/msrest/pipeline/__init__.py", line 197, in run
    return first_node.send(pipeline_request, **kwargs)  # type: ignore
  File "/home/kinori/.local/lib/python3.8/site-packages/msrest/pipeline/__init__.py", line 150, in send
    response = self.next.send(request, **kwargs)
  File "/home/kinori/.local/lib/python3.8/site-packages/msrest/pipeline/requests.py", line 65, in send
    self._creds.signed_session(session)
AttributeError: 'str' object has no attribute 'signed_session'

@saikrishna-2017 I just tried this change and it complains about the str. I tried this date format earlier by passing the string myself too. The error it gives: 'str' object has no attribute 'signed_session'

Can you send full error message?

saikrishna-2017 commented 4 years ago

@KinoriSR you are initializing the anomaly detector client in wrong way anomaly_detector_client = AnomalyDetectorClient(ANOMALY_DETECTOR_ENDPOINT, ANOMALY_DETECTOR_KEY) instead it should be anomaly_detector_client = AnomalyDetectorClient(ANOMALY_DETECTOR_ENDPOINT, CognitiveServicesCredentials(ANOMALY_DETECTOR_KEY))

KinoriSR commented 4 years ago

Oh... missed that! Both using date_parser and the CognitiveServicesCredentials together resolved the issue. Thank you!