

{"id":79295,"date":"2020-07-16T13:02:40","date_gmt":"2020-07-16T07:32:40","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=79295"},"modified":"2021-05-09T13:13:44","modified_gmt":"2021-05-09T07:43:44","slug":"numpy-data-types","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/","title":{"rendered":"NumPy Data Types &#8211;  Object and Parameters"},"content":{"rendered":"<p>Python has a basic range of data types in it. NumPy supports a wider range of data types as compared to Python. The greater variety of data types increase the functionalities of NumPy. The options for data type specification widens for the array elements.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79353\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg\" alt=\"NumPy Data Types\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h2>Data Types in Python<\/h2>\n<p>1. strings- used to represent text data, the data is given in quotes. E.g.\u201dabcd\u201d<br \/>\n2. integer- used for representing numbers. E.g. 1,3<br \/>\n3. float- used for real number representations. E.g. 1.2,5.6<br \/>\n4. boolean- used for true or false type output<br \/>\n5<strong>.<\/strong> complex- used for the complex plain representation of numbers. E.g. 1.0+2.0j, 1.5+2.5j<\/p>\n<h2>NumPy Built-in Data Types<\/h2>\n<p>We can reference the built-in data types in NumPy by particular character code.<\/p>\n<ul>\n<li>i &#8211; integer<\/li>\n<li>b &#8211; boolean<\/li>\n<li>u \u2013 unsigned<\/li>\n<li>f \u2013 float<\/li>\n<li>c \u2013 complex float<\/li>\n<li>m &#8211; timedelta<\/li>\n<li>M &#8211; datetime<\/li>\n<li>O &#8211; object<\/li>\n<li>S &#8211; string<\/li>\n<li>U \u2013 unicode string<\/li>\n<li>V \u2013 void<\/li>\n<\/ul>\n<h2>Numpy Scalar Data Types<\/h2>\n<p><strong>The following scalar data types are available in NumPy:<\/strong><\/p>\n<p>1. bool_ &#8211; It is used to return Boolean true or false values.<br \/>\n2. int_ &#8211; It is the default integer type (int64 or int32)<br \/>\n3. intc &#8211; It is identical to the integer in C (int32 or int64)<br \/>\n4. intp &#8211; It is the integer value used for indexing<br \/>\n5. int8 &#8211; It is for assigning 8-bit integer value (-128 to 127)<br \/>\n6. int16 &#8211; It is for assigning 16-bit integer value (-32768 to 32767)<br \/>\n7. int32 &#8211; It is for assigning 32-bit integer value (-2147483648 to 2147483647)<br \/>\n8. int32 &#8211; It is for assigning 64-bit integer value (-9223372036854775808 to 9223372036854775807)<br \/>\n9. uint8 &#8211; It is for assigning unsigned 8-bit integer value (0 to 255)<br \/>\n10. uint16 &#8211; It is for assigning unsigned 16-bit integer value (0 to 65535)<br \/>\n11. uint32 &#8211; It is for assigning unsigned 32-bit integer value (0 to 4294967295)<br \/>\n12. uint64 &#8211; It is for assigning unsigned 64-bit integer value (0 to 18446744073709551615)<br \/>\n13. float_ &#8211; It is to assign float values.<br \/>\n14. float16 &#8211; It is for half precision float values.<br \/>\n15. float32 &#8211; It is for single-precision float values.<br \/>\n16. float64 &#8211; It is for double-precision float values.<br \/>\n17. complex_ &#8211; It is to assign complex values.<br \/>\n18. complex64 &#8211; It is to represent two 32-bit float complex values (real and imaginary)<br \/>\n19<strong>.<\/strong> complex128 &#8211; It is to represent two 64-bit float complex values (real and imaginary)<\/p>\n<h2>NumPy Dataype Object<\/h2>\n<p>The arrays in Numpy are homogenous in nature. The elements of the array have the same data type. The data type of the elements is given by the dtype object. It is an object comprising of a combination of all the fundamental data types. It has the following syntax.<\/p>\n<p><strong>numpy.dtype(obj, align=False, copy=False)<\/strong><\/p>\n<h3>Parameters<\/h3>\n<p>1. obj<br \/>\nData that has to be converted to a data-type object.<\/p>\n<p>2. align bool, optional<br \/>\nIt is true if we have string or dictionary input.<\/p>\n<p>3. copy bool, optional<br \/>\nWe set this is we want to make a new copy of the data-type object.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">np.dtype(np.int16)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">dtype(&#8216;int16&#8217;)<\/div>\n<h3>Checking the Data Type of an array<\/h3>\n<p>The dtype property is useful to return the data type of the array object<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">arr = np.array([1, 2, 3, 4])\r\n print(arr.dtype)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">int64<\/div>\n<h3>Creating Arrays with a Defined Data Type<\/h3>\n<p>We use the array()function to create arrays, the function can take dtype as an argument that helps define the define the data type of the array elements.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr = np.array([1, 2, 3, 4], dtype='i4')\r\nprint(arr)\r\nprint(arr.dtype)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1 2 3 4]<br \/>\nint32<\/div>\n<h3>Element type casting<\/h3>\n<p>If we give a type to elements that cannot be cast then a value error will generate.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\n \r\narr = np.array(['a', '2', '3'], dtype='i')\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">ValueError Traceback (most recent call last)<br \/>\n&lt;ipython-input-4-3d3a7c165e54&gt; in &lt;module&gt;()<br \/>\n1 import numpy as np<br \/>\n2 &#8212;-&gt;<br \/>\n3 arr = np.array([&#8216;a&#8217;, &#8216;2&#8217;, &#8216;3&#8217;], dtype=&#8217;i&#8217;)<\/div>\n<h3>Changing Data Type of Existing Array<\/h3>\n<p>The most basic method to change the existing data type is to use the method.<br \/>\nThis function creates a copy of the existing array. It then allows specifying the new data type for the copy as a parameter.<br \/>\nThe data type can be specified using the respective character code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">arr = np.array([1.1, 2.1, 3.1])\r\nnewarr = arr.astype('i')\r\nprint(newarr)\r\nprint(newarr.dtype)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1 2 3]<br \/>\nint32<\/div>\n<h2>Summary<\/h2>\n<p>There are a few basic and advanced data types available in NumPy. It also has data types with bit size specification, which helps memory optimization.<\/p>\n<p>There are a few data types that are dependent on the platforms (32-bit or 64-bit). We define data types in a manner to provide the best output in terms of memory and precision.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has a basic range of data types in it. NumPy supports a wider range of data types as compared to Python. The greater variety of data types increase the functionalities of NumPy. The&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":79353,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22401],"tags":[3491,9172,22634],"class_list":["post-79295","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-data-types-in-numpy","tag-numpy-data-types","tag-scalar-data-types-in-numpy"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>NumPy Data Types - Object and Parameters - DataFlair<\/title>\n<meta name=\"description\" content=\"Numpy data types - Learn various data types in numpy, objects and their parameters. Learn various scala data types in python numpy with their syntax example\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/numpy-data-types\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy Data Types - Object and Parameters - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Numpy data types - Learn various data types in numpy, objects and their parameters. Learn various scala data types in python numpy with their syntax example\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/numpy-data-types\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-16T07:32:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:43:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NumPy Data Types - Object and Parameters - DataFlair","description":"Numpy data types - Learn various data types in numpy, objects and their parameters. Learn various scala data types in python numpy with their syntax example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/","og_locale":"en_US","og_type":"article","og_title":"NumPy Data Types - Object and Parameters - DataFlair","og_description":"Numpy data types - Learn various data types in numpy, objects and their parameters. Learn various scala data types in python numpy with their syntax example","og_url":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-07-16T07:32:40+00:00","article_modified_time":"2021-05-09T07:43:44+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"NumPy Data Types &#8211; Object and Parameters","datePublished":"2020-07-16T07:32:40+00:00","dateModified":"2021-05-09T07:43:44+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/"},"wordCount":701,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg","keywords":["Data Types in NumPy","Numpy Data Types","scalar data types in numpy"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/numpy-data-types\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/","url":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/","name":"NumPy Data Types - Object and Parameters - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg","datePublished":"2020-07-16T07:32:40+00:00","dateModified":"2021-05-09T07:43:44+00:00","description":"Numpy data types - Learn various data types in numpy, objects and their parameters. Learn various scala data types in python numpy with their syntax example","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/numpy-data-types\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-Data-Types.jpg","width":1200,"height":628,"caption":"NumPy Data Types"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/numpy-data-types\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"NumPy Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/numpy\/"},{"@type":"ListItem","position":3,"name":"NumPy Data Types &#8211; Object and Parameters"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=79295"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79295\/revisions"}],"predecessor-version":[{"id":93080,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79295\/revisions\/93080"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/79353"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=79295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=79295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=79295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}